I
imanhp
Hi.
I have a small problem here with my try/catch inside a while loop.
Im doing a quick and dirty forced read like so:
-----------------------------------------------------------------
Scanner inp = new Scanner(System.in);
PrintStream out = System.out;
double num = 0;
while(true) {
try { num = inp.nextDouble(); return num; }
catch(Exception e) { out.println("reenter value: "); }
}
------------------------------------------------------------------
The problem is that try clause wont be tried again in the while loop
if a enter a non numeric input the first time.
As if the exception cant be ignored for some reason.
The code will just wind up printing "reenter value: " forever.
Why is this and how do i fix it?
cheers
//iman
I have a small problem here with my try/catch inside a while loop.
Im doing a quick and dirty forced read like so:
-----------------------------------------------------------------
Scanner inp = new Scanner(System.in);
PrintStream out = System.out;
double num = 0;
while(true) {
try { num = inp.nextDouble(); return num; }
catch(Exception e) { out.println("reenter value: "); }
}
------------------------------------------------------------------
The problem is that try clause wont be tried again in the while loop
if a enter a non numeric input the first time.
As if the exception cant be ignored for some reason.
The code will just wind up printing "reenter value: " forever.
Why is this and how do i fix it?
cheers
//iman