Docsity

Exception Handling in Java: Handling NumberFormatException in a Do-While Loop, Lab Reports of Computer Science

Toggle sidebar
bg1
Exception Handlers
File ScanInts.java is a program that contains a do while loop that does the following:
* Read in a string.
* If the length of the string is > 0, convert the string to an integer using Integer.parseInt.
* Add the integer to sum.
After the user enters an empty string as input, the loop stops and the program displays the sum
of the integers.
Save ScanInts.java to C:\|temp and compile and run it. 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.
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. Do this as follows:
* Modify the program to add a try statement that encompasses the entire do while loop.
The try and โ€œ { โ€œ should go before the do and the โ€œ } โ€œ after the while. Catch a
NumberFormatException, and have an empty body for the catch.
* 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. To fix this, move
the try and catch inside the do while loop . Now when an exception is thrown, the next
statement is the next iteration of the loop, so the next input is processed.

Partial preview of the text

Download Exception Handling in Java: Handling NumberFormatException in a Do-While Loop and more Lab Reports Computer Science in PDF only on Docsity!

Exception Handlers

File ScanInts.java is a program that contains a do while loop that does the following:

  • Read in a string.
  • If the length of the string is > 0, convert the string to an integer using Integer.parseInt.
  • Add the integer to sum.

After the user enters an empty string as input, the loop stops and the program displays the sum of the integers.

Save ScanInts.java to C:|temp and compile and run it. If you give it the input

it should print

The sum of the integers is 100.

Try some other inputs as well.

Now try a line that contains both integers and a non-numeric value such as

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. Do this as follows:

  • Modify the program to add a try statement that encompasses the entire do while loop. The try and โ€œ { โ€œ should go before the do and the โ€œ } โ€œ after the while. Catch a NumberFormatException , and have an empty body for the catch.

  • 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. To fix this, move the try and catch inside the do while loop. Now when an exception is thrown, the next statement is the next iteration of the loop, so the next input is processed.