Petefic
Petefic

Reputation: 677

need help formatting strings in Java

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.

Upvotes: 2

Views: 1312

Answers (5)

Buhake Sindi
Buhake Sindi

Reputation: 89169

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();

Upvotes: 0

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236004

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.

Upvotes: 0

RanRag
RanRag

Reputation: 49567

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.

Upvotes: 0

Johan
Johan

Reputation: 318

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 + ".");

Upvotes: 3

Robin
Robin

Reputation: 36611

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

Upvotes: 1

Related Questions