プログラムを実行しようとすると、このエラーが発生します
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' に書き込もうとしています。テキスト ファイルが正しいフォルダーにあること、および読み取る行があることを確認しました。プログラムは、1 行ずつ読み取り、それらの行からオブジェクトを作成することになっています。
これがコードの外観です。どんな助けでも大歓迎です。
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));
}
}