2

I just started learning Java and I'm having trouble formatting string. In the problem I have a string a user inputted that is a name in the format: "First Middle Last". I need to output the string in the format: "Last, First MI. " (MI is middle initial).

Here is what I have so far, I have the first name working, but unsure how to go about getting the last and middle initial out of the string.

// Variable declarations
String name, first, last, middle;
Scanner scan = new Scanner (System.in);

// Get name from user in format "First Middle Last"
System.out.println("Enter the person's name: ");
name = scan.nextLine();

// Get first, middle initial, and last name from the string
first = name.substring(0, name.indexOf(" "));
middle =
last =


// Output formatted name as "Last, First MI."
System.out.println(last + ", " + first + " " + middle + ".");

so for example if the user entered: "John Robert Doe", it would output as "Doe, John R."

Any help is appreciated.

1
  • If this is homework, please tag it as such Commented Jan 28, 2012 at 22:50

5 Answers 5

3

You can use the split method of the String class

// Get first, middle initial, and last name from the string
String nameParts [] = name.split(" ");
// not sure if you need these variables, but I guess you get the picture
first = nameParts [0];
middle = nameParts [1];
last = nameParts [2];
middleInital = middle.charAt(0);

// Output formatted name as "Last, First MI."
System.out.println(last + ", " + first + " " + middleInital + ".");
Sign up to request clarification or add additional context in comments.

Comments

1

Take a look at the String.split method. This allows you to find the substrings. Then you only have to place them in the correct order

Comments

0

Take a look at String split and charAt method of String class.

String person_data = "John Robert Doe" ;
String[] data = person_data.split(" ");

char MI = data[1].charAt(0);

System.out.println(data[2] +","+  data[0] + " "+ MI);

Output = Doe,John R

Here

Data[0] == "John"

Data[1] == "Robert" 

Data[2] == "Doe" 

MI = first character of Data[1] which is R.

Comments

0

Try this:

String name = "First Middle Last";
String[] data = name.split(" ");
String formatted = String.format("%s, %s %c.", data[2], data[0], data[1].charAt(0));

The last line assigns the value "Last, First M." to the variable formatted, as expected. This solution makes use of Java's Formatter class, which is a big help for all your string formatting needs.

Comments

0

You will need to first split the string (using String.split) and then format it.

Forgive me since I'm typing this on my iPad, the answer will look as follows:

String names = name.split("\\s+"); \\Split on whitespaces, including tab, newline and carriage return.
StringBuilder sb = new StringBuilder();
for (int x = 0; x < names.length; x++) {
     switch (x) {
        case 0: sb.apppend(names[names.length - 1]).append(", ");
            break;
        case 1: sb.append(names[0]).append(" ");
            break;
        case 2: sb.append(Character.toUpperCase(names[1].charAt(0))).append(".");
            break;
        default: break;
      }
}

String fullName = sb.toString();

4 Comments

What a convoluted way of writing this.
But its the only version among answers which will work for shorter inputs like "Name Surname" ;) btw. StringBuilder should be used instead of StringBuffer...
@msi, thanks, changed to StringBuilder (even though StringBuffer suffices though) :-)
@msi - a better approach is to detect that the input is invalid (i.e. doesn't conform to the specified input format) and throw an exception. IllegalArgumentException is a good choice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.