Hi people, I'm new here!
I have decided to write a small program to learn myself Java. i have created a function that should catch a mismatch exception and respond by asking for the data to be entered again. However, it does not work when I enter purposefully erroneous data.
When I enter a String (should be an int) the loop goes round constantly. Not sure if its na infinate loop but i always end up resetting the Java Virtual Machine.
The code:
public int doPubYear() { boolean done = false; int pubYear = 0; while(!done){ try{ System.out.print("Year of publication: "); pubYear = keyboard.nextInt(); done = true; } catch(InputMismatchException e){ System.out.println("Error with input - try again."); } } return pubYear; }
I have :-
at the top.import java.util.Scanner; import java.util.InputMismatchException;
Thanks