java.util.NoSuchElementException问题
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)
请问这是怎么回事
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.util.NoSuchElementException(解决方案).md
以下是一个使用try-catch语句块来捕获NoSuchElementException的例子:```javaimport java.util.*; public class Main { public
java.util.pdf
标题“java.util.pdf”暗示这是一个关于Java编程语言中util包的文档。由于描述和标签均重复标题,我们可以推断文档重点在于解释和示例展示java.util包中的类与接口。
MyBatis-Plus批量操作问题[代码]
异常信息通常指向java.util.NoSuchElementException,这是因为在异步线程和打包环境下ServiceLoader无法正确发现实现类,导致MyBatis-Plus的SPI兼容性机制无法正常工作
解析Java中的队列和用LinkedList集合模拟队列的方法
下面是一个使用`LinkedList`模拟队列的简单示例:```javaimport java.util.LinkedList;import java.util.NoSuchElementException
java 自定义Queue队列
同时,还需要处理并发访问时的线程安全问题,如果队列将在多线程环境下使用,可能需要使用`synchronized`关键字或者`java.util.concurrent`包中的锁来保证线程安全。


