gpt4 book ai didi

从文件读取时出现 java.util.InputMismatchException

转载 作者:行者123 更新时间:2023-12-01 22:03:39 28 4
gpt4 key购买 nike

当我从文件中读取以下信息时,出现java.util.InputMismatchException:

ADDRECORD CPCS-204 289 Rayyan Alzahrani 62 58 76

Student s = null;
while (input.hasNext()) {
String command = input.next();
if (command.equals("ADDRECORD")) {
int ID = input.nextInt();
String firstName = input.next();;
String lastName = input.next();
int[] examGrades = new int[nnmCourses];
for (int i = 0; i < nnmCourses; i++) {
examGrades[i] = input.nextInt();
s = new Student(ID, firstName, lastName, examGrades);
System.out.print(nnmCourses);
}

最佳答案

您正在调用

input.nextInt()

但是ADDRECORD之后的下一个标记将是

CPCS-204

您需要先读取此 token ,然后读取 ID 字段。

您可能会发现使用 input.nextLine() 更容易,然后拆分或以其他方式标记该字符串。

关于从文件读取时出现 java.util.InputMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33224902/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com