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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!