

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Lab; Class: Intro Obj Orient Programming; Subject: Computer Science; University: University of Alabama - Birmingham; Term: Unknown 2008;
Typology: Lab Reports
1 / 3
This page cannot be seen from the preview
Don't miss anything!


In this exercise you will:
File ScanInts.java is a program that contains a do while loop that does the following:
After the user enters an empty string as input, the loop stops and the program displays the sum of the integers.
Step 1. Compile and run ScanInts.java. If you give it the input
10 20 30 40
it should print
The sum of the integers is 100.
Try some other inputs as well.
Step 2. Now try a line that contains both integers and a non-numeric value such as
10 2 xyz
You should get a NumberFormatException when the program tries to call Integer.parseInt on "xyz". One way around this is to put the do while loop inside a try, and catch the NumberFormatException but not do anything with it. This way if it is not an integer it does not cause an error; it goes to the exception handler, which does nothing.
Step 3. Modify the program to add a try statement that encompasses the entire do while loop. The try and opening { should go before the do and the } after the while. Catch a NumberFormatException, and have an empty body for the catch.
Step 4. Compile and run the program, and enter some integers and then a non-numeric value. You should find that it stops at the first non-integer. However the problem is that since the entire loop is inside the try, when an exception is thrown the loop is terminated.