0

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());

        }
    }
}
3
  • Because you add only name to List if (!studentName.isEmpty()) Students.add(studentName); Commented Nov 30, 2014 at 20:01
  • @Andrey That is the correct answer. Maybe you could write it as an answer, not a comment; so that I can upvote it, and Anna can accept it. Commented Nov 30, 2014 at 20:02
  • I didn't mean to change your code, I just reformatted it so it is easier to read. And I removed the import statements, since they are not important (well they are important to run the code, but not to show us the code :) Commented Nov 30, 2014 at 20:08

2 Answers 2

3

Because you add only name to List

 if (!studentName.isEmpty()) Students.add(studentName);
Sign up to request clarification or add additional context in comments.

Comments

1

You have to add something like this:

Students.add(String.format("%s %s, id: %s, course: %s", studentName, studentSName, idstudent, course));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.