I am trying to make a simple menu using a do-while loop in java. My code looks like this:
int choice;
Scanner scanChoice = new Scanner(System.in);
do {
System.out.println("Pick an option. 1 2 or 3.");
System.out.println("1. Apple");
System.out.println("2. Pear");
System.out.println("3. Pineapple");
choice = scanChoice.nextInt();
} while (choice < 1 || choice > 3);
System.out.println("You picked " + choice);
The problem is, every time I try to run it, it throws "java.util.NoSuchElementException". The full error is below:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at mainPackage.Main.fruitMenu(Main.java:135)
at mainPackage.Main.main(Main.java:103)
I know that this is because scanChoice.hasNextInt()
returns false, but I'm not sure how to fix this. When I add an if statement ( if (scanChoice.hasNextInt())
), the method scanChoice.hasNextInt()
still returns false, so it just passes over the line that initializes the variable choice
, and that variable never gets initialized.
Anyone know how to fix this?
EDIT: The problem is that it does not wait for the user to input another integer. The function scanchoice.nextInt()
, and the function scanChoice.nextLine()
, both immediately return no value, without waiting for the user to input a value. Any way to make it wait for input?
It seems to work for me, consistently. For any valid integers it works as expected, either accepting the input or goes through a loop, and when typing invalid input like "abcd" it throws InputMismatchException
, that is the expected behavior anyway.
I made your code more robust. It catches alphanumeric characters and displays the menu again, rather than abending with a InputMismatchException.
Here's a test run.
Pick an option. 1 2 or 3.
1. Apple
2. Pear
3. Pineapple
x
Pick an option. 1 2 or 3.
1. Apple
2. Pear
3. Pineapple
asdf
Pick an option. 1 2 or 3.
1. Apple
2. Pear
3. Pineapple
2
You picked 2
And here's the code. I called the Scanner nextLine method.
package com.ggl.testing;
import java.util.Scanner;
public class MenuTest {
public static void main(String[] args) {
int choice;
Scanner scanChoice = new Scanner(System.in);
do {
System.out.println("Pick an option. 1 2 or 3.");
System.out.println("1. Apple");
System.out.println("2. Pear");
System.out.println("3. Pineapple");
String input = scanChoice.nextLine();
choice = convertToInteger(input.trim());
} while (choice < 1 || choice > 3);
System.out.println("You picked " + choice);
scanChoice.close();
}
private static int convertToInteger(String input) {
try {
return Integer.valueOf(input);
} catch (NumberFormatException e) {
return Integer.MIN_VALUE;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.