How to make scanner retry when exception occur?
Consider this app running on CLI mode.
Example:
System.out.print("Define width: ");
try {
width = scanner.nextDouble();
} catch (Exception e) {
System.err.println("That's not a number!");
//width = scanner.nextDouble(); // Wrong code, this bring error.
}
If the user not inputting double
type input, then the error thrown. But i want after the error message appears. It's should be asking the user input the width again.
How to do that?
If I understood you correctly, you want the program to ask the user to re-enter a right input after it fails. In that case you can do something like:
This works perfectly,i have double checked
You can use: