// Bonus program (not covered in lecture) // // This program prompts the user for a string of 3 names // of the form: first.middle.last // The program separates this single string into an array of size three. import java.util.*; public class Names { public static final char SEPARATOR = '.'; public static final int NUM_NAMES = 3; public static void main(String args[]) { Scanner console = new Scanner(System.in); System.out.println("Please enter a string containing " + NUM_NAMES + " names separated by " + SEPARATOR); String inputString = console.next(); String[] names = extractNames(inputString); for(int i = 0; i < NUM_NAMES; i++) { System.out.println("names[" + i + "]: " + names[i]); } } // Accepts a string in the form Thomas.Alva.Edison // and returns an array containing each of the 3 names public static String[] extractNames(String fullName) { // Creates an array of NUM_NAMES strings String[] names = new String[NUM_NAMES]; // Get all but the last name for (int i = 0; i < NUM_NAMES - 1; i++) { int separatorLoc = fullName.indexOf(SEPARATOR); names[i] = fullName.substring(0, separatorLoc); // consume a name and a separator fullName = fullName.substring(separatorLoc + 1, fullName.length()); } // Get last name names[NUM_NAMES-1] = fullName; return names; } }