Hi guys, i wish to rerun the loop should if the exception is caught, and i could'nt think of a way to do this.
Right now if the exception is caught a sys.err prints then i have an infinite loop that which i have to force kill to end the program.
heres the code fairly short one, took out just for the purpose of focusing on this matter.
Program runs fine should the input is good. Only when the exception is caught i could not think of a way to handle it.
thanks in advance
[code]
public static void main(String[] args) {
// TODO Auto-generated method stub
int hourin, maxHour=23;
boolean bool = true;
Scanner sc2 = new Scanner(System.in); // Requires J2SE 1.5
do {
try{
System.out.println("Enter hour ? : 00 - 23 " );
hourin = sc2.nextInt();
if( hourin > maxHour ){
System.out.println("Hour value not valid: 00 -23");
System.out.println("Please enter again");
}else bool = false;
}catch (NumberFormatException ex) {
System.err.println("Not a valid number: " + ex);
return;
}
catch(InputMismatchException ex){
System.err.println("Not a valid number: " + ex);
return;
}
} while(bool);
}
}
[code]
2 Replies - 4764 Views - Last Post: 11 March 2010 - 11:04 PM
#1
how to reiterate a loop after an exception caught within a do while
Posted 11 March 2010 - 10:18 PM
Replies To: how to reiterate a loop after an exception caught within a do while
#2
Re: how to reiterate a loop after an exception caught within a do while
Posted 11 March 2010 - 10:50 PM
jason89, on 11 March 2010 - 09:18 PM, said:
Hi guys, i wish to rerun the loop should if the exception is caught, and i could'nt think of a way to do this.
Right now if the exception is caught a sys.err prints then i have an infinite loop that which i have to force kill to end the program.
heres the code fairly short one, took out just for the purpose of focusing on this matter.
Program runs fine should the input is good. Only when the exception is caught i could not think of a way to handle it.
thanks in advance
Right now if the exception is caught a sys.err prints then i have an infinite loop that which i have to force kill to end the program.
heres the code fairly short one, took out just for the purpose of focusing on this matter.
Program runs fine should the input is good. Only when the exception is caught i could not think of a way to handle it.
thanks in advance
public static void main(String[] args) { // TODO Auto-generated method stub int hourin, maxHour=23; boolean bool = true; Scanner sc2 = new Scanner(System.in); // Requires J2SE 1.5 do { try{ System.out.println("Enter hour ? : 00 - 23 " ); hourin = sc2.nextInt(); if( hourin > maxHour ){ System.out.println("Hour value not valid: 00 -23"); System.out.println("Please enter again"); }else bool = false; }catch (NumberFormatException ex) { System.err.println("Not a valid number: " + ex); return; } catch(InputMismatchException ex){ System.err.println("Not a valid number: " + ex); return; } } while(bool); } }
here is a working solution:
import java.util.InputMismatchException; import java.util.Scanner; public class s { public static void main(String[] args) { int hourin, maxHour=23; Scanner sc2 = new Scanner(System.in); // Requires J2SE 1.5 boolean bool = true; boolean keepLooping; // if exception is thrown, we set this to be true. do { keepLooping = false; try { System.out.println("Enter hour ? : 00 - 23 " ); hourin = sc2.nextInt(); if( hourin > maxHour ) { System.out.println("Hour value not valid: 00 -23"); System.out.println("Please enter again"); } else bool = false; } catch (NumberFormatException ex) { System.out.println("Not a valid number: "); keepLooping = true; // we need to loop again sc2.next(); // eat the '\n' } catch(InputMismatchException ex) { System.out.println("Not a valid number: "); keepLooping = true; // we need to loop again sc2.next();// eat the '\n' } } while(bool || keepLooping); } }
#3
Re: how to reiterate a loop after an exception caught within a do while
Posted 11 March 2010 - 11:04 PM
Aha, brilliant stuff thanks alot.
Page 1 of 1