I have created this code to save students info to txt file but it only saves the name. I can't find what's wrong with this any ideas? I'm not getting any errors programma runs just fine expect some error handling and other things i have to add.
Main class
public class Main {
public static void main(String[] args) throws IOException {
File fileName = new File("Students.txt");
ArrayList Students = new ArrayList();
String studentName = " ";
String studentSName = " ";
String idstudent = " ";
String course = " ";
while (!studentName.isEmpty()) {
studentName = JOptionPane.showInputDialog("Student's Name: ");
studentSName = JOptionPane.showInputDialog("Student's Surname: ");
idstudent = JOptionPane.showInputDialog("Student's IDnumber: ");
course = JOptionPane.showInputDialog("Student's Course: ");
if (!studentName.isEmpty()) {
Students.add(studentName);
}
}
try {
FileWriter fw = new FileWriter(fileName);
Writer output = new BufferedWriter(fw);
int sz = Students.size();
for (int i = 0; i < sz; i++) {
output.write(Students.get(i).toString() + "\n");
}
output.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "file not found");
}
}
}
2nd class:
public class filereading {
public static void main(String[] args) {
String FileName = "students.txt";
String line;
ArrayList Students = new ArrayList();
try {
BufferedReader input = new BufferedReader(new FileReader(FileName));
if (!input.ready()) {
throw new IOException();
}
while ((line = input.readLine()) != null) {
Students.add(line);
}
input.close();
} catch (IOException e) {
System.out.println(e);
}
int sz = Students.size();
for (int i = 0; i < sz; i++) {
System.out.println(Students.get(i).toString());
}
}
}
if (!studentName.isEmpty()) Students.add(studentName);