previous | index | next

Step 2: Handle Non-Integer Input

Now modify the program so that incorrectly formatted integers are handled:
    How old are you? foo
    Bad integer.
    How old are you? bar
    Bad integer.
    How old are you? baz
    Bad integer.
    How old are you? 13
    Next year, you'll be 14

This is accomplished by catching the NumberFormatException that can be thrown by the Integer.parseInt call in the getAge method. Recursion can be exploited to cause the looping behavior. Note: do not handle the bad input "ioerror" in this step.


previous | index | next