java.util.NoSuchElementException问题

恭喜WE 2019-03-24 03:40:04
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)
请问这是怎么回事
...全文
905 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧