| Author |
Looping try/catch
|
Joel Christophel
Ranch Hand
Joined: Apr 20, 2011
Posts: 119
|
|
When I run the below method and intentionally input a letter when a number is expected, I get a never-ending loop that doesn't prompt for more input. Why?
|
 |
Rene Ott
Greenhorn
Joined: Apr 20, 2012
Posts: 8
|
|
Your condition is to loop until itsAnInt ==true, lets start looping.
You enter character and then set itsAnInt =true, but because you entered character you catch exception.
after you catch exception you set itsAnInt = false. Program's next step is to check while loop's condition, which was continue until itsAnInt ==true, but we had itsAnInt = false.
|
 |
Joel Christophel
Ranch Hand
Joined: Apr 20, 2011
Posts: 119
|
|
Well, I tried removing the last "itsAnInt = false" and it didn't change a thing. I also know that itsAnInt is never made true when a non-numerical character is typed in. Here's the code I used to test that:
|
 |
Joel Christophel
Ranch Hand
Joined: Apr 20, 2011
Posts: 119
|
|
I found the answer to my problem elsewhere and felt I should share it here. nextLine() is needed to clear the Scanner. Here's the code:
|
 |
 |
|
|
subject: Looping try/catch
|
|
|