//################################################################ // // FormattingNames.java // // Sample Run: // // Please enter your three names: First Middle Last // // Debugging Information: // You entered: [First Middle Last] // First Space is at Index = 5 // First Name = [First] // Second Space is at Index = 12 // Second Name = [Middle] // Third Name = [Last] // // This is the formatted name: // LAST, First M. // //################################################################ import java.util.Scanner; public class FormattingNames { public static void main (String[] args) { Scanner scan = new Scanner(System.in); // Reads the names, assuming this format: First Middle Last System.out.print("Please enter your three names: "); String allNames= scan.nextLine(); System.out.println("\nDebugging Information:"); System.out.println("You entered: [" + allNames + "]"); // Extract the first name int firstSpaceIdx = allNames.indexOf(' '); System.out.println("First Space is at Index = " + firstSpaceIdx); String firstName= allNames.substring(0, firstSpaceIdx); System.out.println("First Name = [" + firstName + "]"); // Extract the Second Name int secondSpaceIdx = allNames.indexOf(' ', firstSpaceIdx + 1); System.out.println("Second Space is at Index = " + secondSpaceIdx); String secondName= allNames.substring(firstSpaceIdx + 1, secondSpaceIdx); System.out.println("Second Name = [" + secondName + "]"); // Extract the third name String thirdName = allNames.substring(secondSpaceIdx + 1, allNames.length()); System.out.println("Third Name = [" + thirdName + "]"); // Print the name in the format: LAST, First M. System.out.println("\nThis is the formatted name:"); System.out.print(thirdName.toUpperCase()); System.out.print(", "); System.out.print(firstName); System.out.print(" "); System.out.print(secondName.charAt(0)); System.out.print("."); System.out.print("\n"); } }