Write a program that collects a full name as one string input.

Format and output the name as shown below:

lastInitial., firstName middleInitial.

If no middle name was provided, format and output the name as shown below:

lastInitial., firstName

If the input is:

Pat Silly Doe

the output is:

D., Pat S.

Alternatively, if the input is:

Julia Clark

the output is:

C., Julia

Solution


public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);       
      /* Type your code here. */
      String fullName = scnr.nextLine();
      String[] nameParts = fullName.split(" ");
      char lastInitial = nameParts[nameParts.length - 1].charAt(0);

      System.out.print(lastInitial + "., " + nameParts[0]);

      if (nameParts.length == 3) {
			char middleInitial = nameParts[1].charAt(0);
      System.out.print(" " + middleInitial + ".");
      }

        System.out.println(); 
        
   }
}