Vote count:
0
My program is supposed to accept a floating-point variable and then exit. However I am practicing some exception handling stuff and found a problem. Whenever you enter a letter into this program the program of course throws an InputMismatchException but it gets stuck inside an infinite loop. I assume my problem is based off of my misunderstanding of try-catch statements and exception handling.
public static void main(String [] args){
Scanner reader = new Scanner(System.in);
boolean done = false;
do{
try{
System.out.print("Enter a number: ");
float number = reader.nextFloat();
done = true;
}
catch (Exception e){
System.out.println("uh oh");
}
}while(!done);
}
This problem does not occur if I use a different variable type so I'm not sure if it's a logical error or just something funky with floating-point variables.
asked 32 secs ago
Floating-Point Variables and Exception Handling
Aucun commentaire:
Enregistrer un commentaire