COP 3337 Section U05 Spring 2017 The Largest Integer =================== I. The Program: --------------- import javax.swing.JOptionPane; public class Spring17COP3337Exceptions { // compute the largest of a sequence of integers // the user enters the data from the JOptionPane public static void main(String[] args) { System.out.println("The Largest of a Sequence of Integers"); System.out.println("=====================================\n"); String input = JOptionPane.showInputDialog("Enter an integer, click " + "on Cancel to finish."); // echo print the numbers System.out.println("\nThe integers:"); System.out.println("-------------\n"); // noIntegers is true as long as we haven't found an integer boolean noIntegers = true; int largest = Integer.MIN_VALUE; // the current largest number // input is null when the user clicks on Quit while (input != null) { // try to get an integer try { int current = Integer.parseInt(input); System.out.printf("%10d\n",current); if (noIntegers) { // initialize largest and set nointegers to false largest = current; noIntegers = false; } else // largest is defined if (current > largest) largest = current; // update largest input = JOptionPane.showInputDialog("Enter an integer, click " + "on Cancel to finish."); } catch (NumberFormatException e) { System.out.println("Error: " + input + " is not an integer."); input = JOptionPane.showInputDialog(input + " is not " + "an integer. Try again or click on Cancel to finish."); } } // print the largest if (noIntegers) System.out.println("There are no integers, the largest is undefined."); else System.out.format("\nThe largest is %10d", largest); System.out.println(); } // end main } II. The first Run: ------------------ run: The Largest of a Sequence of Integers ===================================== The integers: ------------- Error: baba is not an integer. 27 Error: 15.3 is not an integer. 25 65 Error: ali baba is not an integer. 15 The largest is 65 BUILD SUCCESSFUL (total time: 1 minute 9 seconds) III. The Second Run ------------------- run: The Largest of a Sequence of Integers ===================================== The integers: ------------- There are no integers, the largest is undefined. BUILD SUCCESSFUL (total time: 3 seconds) III. The Same Program Using the Console --------------------------------------- // import the scanner import java.util.Scanner; public class Spring17COP3337Exceptions2 { // compute the largest of a sequence of integers // the user enters the data from the console public static void main(String[] args) { System.out.println("The Largest of a Sequence of Integers"); System.out.println("=====================================\n"); Scanner console = new Scanner(System.in); // echo print the numbers System.out.println("\nThe integers:"); System.out.println("-------------\n"); // noIntegers is true as long as we haven't found an integer boolean noIntegers = true; int largest = Integer.MIN_VALUE; // the current largest number // input is null when the user clicks on Quit while (true) { // try to get an integer System.out.print("Enter an integer, a non-integer to finish > "); if (console.hasNextInt()) { int current = console.nextInt(); // read the next int if (noIntegers) { noIntegers = false; // initialize largest largest = current; } else // largest was initialized { // update largest if (current > largest) largest = current; } } // end outer if else break; // get out of the loop } // print the largest if (noIntegers) System.out.println("There are no integers, the largest is undefined."); else System.out.format("\nThe largest is %5d\n", largest); } // end main } IV. The Output Of the Second Program: ------------------------------------- run: The Largest of a Sequence of Integers ===================================== The integers: ------------- Enter an integer, a non-integer to finish > 16 Enter an integer, a non-integer to finish > -18 Enter an integer, a non-integer to finish > 29 Enter an integer, a non-integer to finish > 17 Enter an integer, a non-integer to finish > AliBaba The largest is 29 BUILD SUCCESSFUL (total time: 28 seconds)