java.util.NoSuchElementException问题

「已注销」 「已注销」 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
public static void addStudent(ArrayList<Student> array,String fileName) throws IOException {

Scanner sc = new Scanner(System.in);
readData(array, fileName);//调用读取集合的方法
Student s = new Student();
System.out.println("请输入要添加的学生的学号:");
studentID = sc.nextLine();

for(int index2 = 0;index2<array.size();index2++) {
s = array.get(index2);
if(studentID==s.getStudentID()) {
System.out.println("您输入的学号重复,请重新输入");
break;
}
else {
}
}
System.out.println("请输入要添加的学生的姓名:");
name = sc.nextLine();
System.out.println("请输入要添加的学生的年龄:");
age = sc.nextLine();
System.out.println("请输入要添加的学生的居住地:");
address = sc.nextLine();
s.setStudentID(studentID);
s.setName(name);
s.setAge(age);
s.setAddress(address);
array.add(s);
System.out.println("添加成功");
sc.close();
writeData(array, fileName);

}
writeData(array, fileName)方法如下:
public static void writeData(ArrayList<Student> array,String fileName) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
for(int index = 0;index<array.size();index++) {
Student s = array.get(index);
StringBuilder sb = new StringBuilder();
//拼接字符串
sb.append(s.getStudentID()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
bw.write(sb.toString());
bw.newLine();
}
bw.close();
}

报的错误是:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
请问这是怎么回事
...全文

相关推荐

评论
收藏 已收藏
分享
返回顶部
「已注销」
Java 51,407 成员 86,127 内容