Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions