Question
Java: Write an executable program that has just a main method. In main: create an instance of StringBuilder that initially holds an empty string. append
Java:
Write an executable program that has just a main method. In main:
- create an instance of StringBuilder that initially holds an empty string.
- append your first name to the string.
- append your surname to the string.
- display your name.
- insert your middle initial where it should be and spaces as needed. If you don't have a middle name, fake it.
- display your name with middle initial.
- delete your initial and the space(s).
- display your name in reverse order.
SAMPLE OUTPUT
John Public John J Public cilbuP nhoJ
My code is not displaying the same as the same output. If you could fix it and explain what to do that would be appreciated. Here is my code so far:
package smith1090;
import java.util.Scanner;
public class Name {
public static void main(String[] args) { Scanner input = new Scanner(System.in); StringBuffer sb = new StringBuffer(""); System.out.println("Please enter the first name: "); String firstName = input.nextLine(); System.out.println("Please enter your middle initial: "); String middleInitial = input.nextLine(); System.out.println("Please enter your last name: "); String lastName = input.nextLine(); sb.append(firstName + " "); sb.append(lastName); System.out.println(sb); sb.insert(sb.indexOf(firstName) + firstName.length() + 1, middleInitial + " "); System.out.println(sb); sb.delete(sb.indexOf(middleInitial), sb.indexOf(middleInitial) + 2); System.out.println(sb); System.out.println(sb.reverse());
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started