当我尝试运行我的程序时出现此错误
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1516)
at studenttextwrite.StudentDAO.open(StudentDAO.java:37)
at studenttextwrite.StudentTextWrite.main(StudentTextWrite.java:33)
Java Result: 1
我正在尝试将对象写入 txt 文件“student.txt”。我检查了文本文件是否在正确的文件夹中,并且我有要读取的行。该程序应该逐行读取,然后从这些行创建一个对象。
这是代码的样子,任何帮助将不胜感激,谢谢。
public class StudentDAO implements DAO {
ArrayList<Student> studentList = new ArrayList();
String outputFileName = "student.txt";
File outputFile = new File(outputFileName);
Scanner in;
public StudentDAO() throws DAOException {
try {
in = new Scanner(new BufferedReader(new FileReader(outputFile)));
} catch (FileNotFoundException ex) {
throw new DAOException(ex.getMessage());
}
}
@Override
public void open() {
while (in.hasNextLine()) {
String studentName = in.nextLine();
String studentClass = in.nextLine();
String teacher = in.nextLine();
String studentAge = in.nextLine();
int studentAgeInt = Integer.parseInt(studentAge);
studentList.add(new Student(studentName, studentClass, teacher,
studentAgeInt));
}
}