此错误似乎是一个非常普遍的问题。我已经查找了其他询问此问题并尝试实施其解决方案的堆栈溢出帖子,但我仍然收到相同的错误。完整的错误是:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
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 src.file.main(file.java:29)
我确信我缺少了一些非常简单的东西,但我无法找到它,因为当我通读代码时,逻辑似乎很好。这是我的文件:
public class file {
public static void main(String[] args) throws IOException {
int choice = 0;
Scanner myVal = new Scanner(System.in);
while(choice != 3) {
System.out.println("1. enter info \n2. print info \n3.exit");
System.out.println("Enter a choice: ");
choice = myVal.nextInt(); //Line 29
if(choice == 1) {
enterInfo();
}
else if(choice == 2) {
print();
}
else if(choice == 3) {
System.exit(0);
}
}
}
static ArrayList<newType> studInfo = new ArrayList<src.newType>();
static void enterInfo() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the information (Program year average lastname): ");
String info = keyboard.nextLine().trim();
if(info.isEmpty()) {
System.out.println("Error, no input was made.");
keyboard.close();
return;
}
String[] tokens = info.split(" ");
int size = tokens.length;
String prgm = tokens[0];
int yr = Integer.parseInt(tokens[1]);
String lastname = tokens[3];
Double avg = Double.parseDouble(tokens[2]);
newType inf = new newType(prgm, yr, avg, lastname);
studInfo.add(inf);
System.out.println("Information added.");
keyboard.close();
return;
}
示例输入:math 5 76 Smith,此信息被添加到<code>newType</code>类型的数组列表中,可以在其中打印,也可以添加其他配置文件。
程序编译没有错误或警告,我可以成功运行程序。当我选择选项1时,我以正确的格式输入所有信息,我得到了添加信息的
消息,表明这是一个成功的过程。在这条消息之后,出现了异常。这让我相信错误实际上并不像我最初想的那样存在于我的entInfo函数中,而是当它第二次到达第29行时。我不知道如何修复这个错误,有人能帮忙吗?提前感谢!
除了在使用Scanner时犯的错误之外,Java中遵循的标准也缺失。
请参考为什么我们不应该在一个程序中使用多个扫描仪:为什么关闭扫描仪似乎会破坏新的扫描仪?
我对你的代码做了一些改动。希望这个有用!!!
//修改代码
public class File {
static Scanner myVal = new Scanner(System.in); // Use this scanner throughout the program
static ArrayList<newType> studInfo = new ArrayList<src.newType>();
public static void main(String[] args) throws IOException {
int choice = 0;
while (choice != 3) {
System.out.println("1. enter info \n2. print info \n3.exit");
System.out.println("Enter a choice: ");
choice = myVal.nextInt(); // Line 29
if (choice == 1) {
enterInfo();
} else if (choice == 2) {
// print();
} else if (choice == 3) {
System.exit(0);
}
}
myVal.close(); // Can be closed once the need is over.
}
static void enterInfo() {
// Scanner keyboard = new Scanner(System.in); No need of multiple scanners.
System.out.println("Enter the information (Program year average lastname): ");
String info = myVal.nextLine().trim();
if (info.isEmpty()) {
System.out.println("Error, no input was made.");
// keyboard.close();
return;
}
String[] tokens = info.split(" ");
int size = tokens.length;
String prgm = tokens[0];
int yr = Integer.parseInt(tokens[1]);
String lastname = tokens[3];
Double avg = Double.parseDouble(tokens[2]);
newType inf = new newType(prgm, yr, avg, lastname);
studInfo.add(inf);
System.out.println("Information added.");
// keyboard.close();
return;
}
}