/* T01n17.java -- Showing what will happen if you try to change a formal * parameter. Java lets you do it (it treats formal parameters as local * variables), but the change has no effect on the formal parameter's * corresponding actual parameter. In other words, the actual parameter * is copied into the formal parameter when the method is invoked, and * that's the end of their association for this invocation. */ import java.util.*; public class T01n17 { public static void main (String [] args) { int itemsBought; // Items purchased by a customer at a store String rawInput; // Input from the user, as a String, not an int Scanner keyboard = new Scanner (System.in); // source of input do { System.out.print("\nHow many items were purchased? : "); rawInput = keyboard.nextLine(); } while (!validInteger(rawInput)); itemsBought = Integer.parseInt(rawInput); // farewell(itemsBought); System.out.println("\t\tWe tried to change the formal arguments in\n" + "\t\tboth methods. Back in main(), 'rawInput' now" + " references \"" + rawInput + "\"\n\t\twhile " + "'itemsBought' is now [" + itemsBought + "]." + " Nothing changed!\n"); } /* validInteger(string) -- Checks to see if the user's input * contains only digits. Returns 'true' if so, 'false' o.w. */ private static boolean validInteger (String userInput) { boolean valid = true; // Our default assumption for (int i=0; i