In order to stay close to what has already been done, since the exception fell into the invalid content, we can clear the scanner from incorrect input. input.nextLine()will read invalid input. Doing nothing with the value caught ignores it.
Solution:
Just add input.nextLine();catch to your block.
try {
System.out.print("Enter payrate: "); //ask for payratea
payrate = input.nextFloat(); //store input from console
} catch (Exception InputMismatchException) {
System.out.println("Payrate must be > 0.");
payrate = 0;
input.nextLine();
}
source
share