import java.util.Scanner; /** This is a Java application that, given as input a name in the form ** ** ** ** (where each of the names is a non-empty sequence of non-space characters ** and a single space occurs on either side of the middle name), produces ** as output the same name, but converted into the form ** ** , . ** ** For example, provided as input ** ** "Mary Barbara Smith" ** ** the program will produce as output ** ** "Smith, Mary B." ** ** It is different from the original version of this application ** in that it makes more extensive use of methods. */ public class NameConversion2 { // Create a Scanner that interprets input entered on the keyboard. private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { // Declare symbolic constant for special characters. final char DOLLAR_SIGN = '$'; String userPrompt = "Enter name (in format ): "; String inName = getUserResponse(userPrompt); String outName = convertedName(inName); // Print a message that reports the input and output names. // Dollar signs are shown on either side of each name for the purpose // of verifying that no leading or trailing spaces exist within them. System.out.println("Input name: " + DOLLAR_SIGN + inName + DOLLAR_SIGN); System.out.println("Output name: " + DOLLAR_SIGN + outName + DOLLAR_SIGN); } /* Given (via its argument) a name in the form , ** returns the same name, but converted into the form ** , . */ private static String convertedName(String name) { // Declare symbolic constants for special characters. final char SPACE = ' '; final char COMMA = ','; final char PERIOD = '.'; // Determine the position of the space in between first and middle names. int posOfFirstSpace = name.indexOf(SPACE); // The first name is the prefix ending at the position preceding // the first occurrence of a space. String firstName = name.substring(0, posOfFirstSpace); // The middle initial is the character at the position immediately // following the first occurrence of a space. char midInit = name.charAt(posOfFirstSpace + 1); // The last name is the suffix beginning at the position immediately // following the last occurrence of a space. String lastName = name.substring(name.lastIndexOf(SPACE)+1, name.length()); // Note: Rather than finding the position of the last occurrence of a // space (as we did by using lastIndex() above), we instead could have // found the position of the second occurrence of a space by using // indexOf() as follows: name.indexOf(SPACE, posOfFirstSpace+1) . // (We are assuming that there are exactly two spaces in the given // name, so the second occurrence of a space coincides with the last // occurrence.) // The string to be returned is obtained by concatenating lastName, // comma, etc., etc. return lastName + COMMA + SPACE + firstName + SPACE + midInit + PERIOD; } /* Prompts the user (by displaying the String provided via the argument) ** and returns the string entered at the keyboard in response. */ private static String getUserResponse(String prompt) { // Display the provided prompt. System.out.print(prompt); // Read and return the user's response. return keyboard.nextLine(); } }