InputMismatchException 需要更多输入?
InputMismatchException need more inputs?
我收到 InputMismatchException
java ShoppingTime Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at ShoppingTime.main(ShoppingTime.java:88)
根据我的理解,这是因为我没有在我的主要功能中提供足够的输入?但是我迷失了我需要添加的内容。这会是另一个价格输入吗?我不这么认为,因为我只接受字符串输入和整数输入。
public class ShoppingTime{
public static void main(String args[]) throws FileNotFoundException {
File read = new File("Products.txt");
Scanner sc = new Scanner(read);
ArrayList<Product> ar = new ArrayList<Product>();
while (sc.hasNextLine()) {
String product = sc.nextLine();
int price = sc.nextInt();
ar.add(new Product(product, price));
}
int ch;
Cart c = new Cart();
sc = new Scanner(System.in);
do {
System.out.println("Menu");
System.out.println("1 Add item");
System.out.println("2 View Cart");
System.out.println("3 Clear Cart");
System.out.println("4 Checkout");
System.out.println("5 Exit");
ch = sc.nextInt();
if (ch == 1) {
c.addItem(ar);
}
if (ch == 2) {
c.viewCart();
}
if (ch == 3) {
c.clearCart();
}
if (ch == 4) {
c.checkOut();
}
} while (ch != 5);
}
}
所有行都是一个字符串,因此您已经阅读了整行,包括第一个 nextLine() 中的包装 int call.you 应该只使用 nextLine() 读取并从中解析出 int。
我收到 InputMismatchException
java ShoppingTime Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at ShoppingTime.main(ShoppingTime.java:88)
根据我的理解,这是因为我没有在我的主要功能中提供足够的输入?但是我迷失了我需要添加的内容。这会是另一个价格输入吗?我不这么认为,因为我只接受字符串输入和整数输入。
public class ShoppingTime{
public static void main(String args[]) throws FileNotFoundException {
File read = new File("Products.txt");
Scanner sc = new Scanner(read);
ArrayList<Product> ar = new ArrayList<Product>();
while (sc.hasNextLine()) {
String product = sc.nextLine();
int price = sc.nextInt();
ar.add(new Product(product, price));
}
int ch;
Cart c = new Cart();
sc = new Scanner(System.in);
do {
System.out.println("Menu");
System.out.println("1 Add item");
System.out.println("2 View Cart");
System.out.println("3 Clear Cart");
System.out.println("4 Checkout");
System.out.println("5 Exit");
ch = sc.nextInt();
if (ch == 1) {
c.addItem(ar);
}
if (ch == 2) {
c.viewCart();
}
if (ch == 3) {
c.clearCart();
}
if (ch == 4) {
c.checkOut();
}
} while (ch != 5);
}
}
所有行都是一个字符串,因此您已经阅读了整行,包括第一个 nextLine() 中的包装 int call.you 应该只使用 nextLine() 读取并从中解析出 int。