为什么会抛出java.util.InputMismatchException?
这是我的密码:为什么会抛出java.util.InputMismatchException?,java,Java,这是我的密码: public static void readFile() throws FileNotFoundException { Scanner scan = new Scanner(file); scan.useDelimiter("/|\\."); scan.nextLine(); GrocerieList.foodList.add(scan.next()); GrocerieList.food
public static void readFile() throws FileNotFoundException {
Scanner scan = new Scanner(file);
scan.useDelimiter("/|\\.");
scan.nextLine();
GrocerieList.foodList.add(scan.next());
GrocerieList.foodAmount.add(scan.nextInt());
System.out.println(GrocerieList.foodList); // this is for testing
System.out.println(GrocerieList.foodAmount); // also for testing
}
这是foodAmount的代码:
static ArrayList<Integer> foodAmount = new ArrayList<>();
现在在我看来不应该有不匹配,因为grocereilist.foodAmount.add(scan.nextInt())代码>清楚地拾取了两个整数
scan.useDelimiter("[/|\\.\n]");
修复它。
如果您在添加到foodList后还添加了一个scan.nextLine()
,以消耗时段。
看
这修复了错误。确实,您当前只读取和存储面包,只读取3而不是6。但是scan.nextInt()
正在尝试读取整数值。如果它看到两个,它应该返回哪一个数字?我也有这个想法-但是如果在delimiterfoodList是字符串的ArrayList之前.txt只有一个整数值,就会引发相同的异常?是的,foodList是字符串的ArrayList!
scan.useDelimiter("[/|\\.\n]");