Whenever I try to run this loop, I receive:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at test.readNumber(Lab2.java:28)
at test.go(Lab2.java:15)
at test.main(Lab2.java:7)
I'm trying to repeatedly prompt the user until the keyboard input is positive. Can anyone tell me how I can go about doing that without running into error?
public int readNumber() {
int x = keyboard.nextInt();
while (x < 0) {
System.out.println("Please enter a positive number.");
x = keyboard.nextInt();
}
return x;
}