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)
请问这是怎么回事