Question: Java Quiz only have 30 minutes. Please Help: Given the following segment of code, add try-catch to handle invalid input i.e. non-integer inputs. Such an

Java Quiz only have 30 minutes. Please Help:

Java Quiz only have 30 minutes. Please Help: Given the following segment

Given the following segment of code, add try-catch to handle invalid input i.e. non-integer inputs. Such an input should be reported and not processed. The program should then proceed to get next input. For full credit, you must use a proper exception class instead of a generic one. You don't need to add import statements. Scanner stdIn = new Scanner(System.in); List list = new ArrayList; String inputStr = null; do { System.out.print("Enter a whole number (a to quit): "); inputStr = stdin.next(); if (inputStr.equals("q")) break; int num = Integer.parseInt(inputStr); list.add(num); } while ( true); System.out.println("Entered " + list.size() + " numbers: " + list); With try-catch the segment of code may work like this, assuming it will be embedded in a correct program (italic indicates user input): Enter a whole number (a to quit): 10 Enter a whole number (a to quit): 4.5 Invalid input. No processing. Enter a whole number (a to quit): 4 Enter a whole number (a to quit): 5.6 Invalid input. No processing. Enter a whole number (a to quit): -1 Enter a whole number (a to quit): 9 Entered 3 numbers: (10, 4, -1]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!