Troube Looping "try/catch"
When I run the below method and intentionally input a letter when a number is expected, I get a repeating loop. Why?
Code:
private static int intInput() //Asks the user for an int and returns the input.
{
Scanner genericIntScan = new Scanner(System.in);
boolean itsAnInt = false;
int genericInt = 0;
while(itsAnInt==false)
{
try
{
genericInt = genericIntScan.nextInt();
itsAnInt = true;
}
catch(InputMismatchException ime)
{
System.out.println("\nYou're supposed to type in a number. Try again.");
itsAnInt = false;
}
}
return genericInt;
}
Re: Troube Looping "try/catch"
The nextInt() method fails and doesn't read anything from the input stream; so the next time you try to read an int, the same offending character is read and the method throws an exception; and again and again and again. A cure is to read (and skip) the entire current line in the catch part.
kind regards,
Jos
Re: Troube Looping "try/catch"
What do you mean that the same offending character is read? The second time, shouldn't the Scanner re-prompt for input to get a value for genericInt?
Re: Troube Looping "try/catch"
Quote:
Originally Posted by
joelamos
What do you mean that the same offending character is read?
Exactly as I wrote: the offending character isn't read and stays in the input buffer; it'll be read again by the nextInt() method ad nauseam, no matter how much you prompt for new input.
kind regards,
Jos
Re: Troube Looping "try/catch"
Quote:
Originally Posted by
JosAH
A cure is to read (and skip) the entire current line in the catch part.
Sorry for being a bother, but I also don't understand what you mean by read and skip. Also, it's necessary that I get an int value for genericInt via the user.
Re: Troube Looping "try/catch"
Quote:
Originally Posted by
joelamos
Sorry for being a bother, but I also don't understand what you mean by read and skip. Also, it's necessary that I get an int value for genericInt via the user.
Fine, but what happens when the user types "foo" or another non-number is that the nextInt() method refuses to read those characters and throws an exception. In your catch block you want to get rid of those characters from the input buffer; simply read the entire line (with the offending character(s)) and forget about the line; only then you try to try a nextInt() call so that it reads fresh characters.
kind regards,
Jos
Re: Troube Looping "try/catch"
I'm not exactly sure how to go about reading the line with the offending characters. How do I access it?
Re: Troube Looping "try/catch"
Quote:
Originally Posted by
joelamos
I'm not exactly sure how to go about reading the line with the offending characters. How do I access it?
Read the API documentation for the Scanner class; it has a nextLine() method that reads an entire line for you. Read a line and forget about it because it contains the offending character(s) which you already know because your code ended up in the catch block.
kind regards,
Jos
Re: Troube Looping "try/catch"
Thanks so much for all your help. I finally got it working! :)