0

i am trying to read a csv file and store the data in the csv file into the arraylist. However when i run the program the error keep saying that i have arrayindex out of bounds.

the csv file contains -> C,John,Goh,0031

public static boolean processNewData(ArrayList<Student> studList, String filename) {
    File infile = new File(filename);
    Scanner reader = null;

    boolean addStudent = true;
    boolean success = false;

    try {
        reader = new Scanner(infile);

        while (reader.hasNextLine()) {
        String row = reader.nextLine();
        String[] data = row.split(",");
                
        if (studList.size() == 0) {
            addStudent = true;
        } else if (studList.size() > 0) {
            // Check if student number exist in the student arraylist
            for (Student s : studList) {
                if (s.getStudNo() == Long.parseLong(data[3])) {
                    addStudent = false;
                    throw new StudentException("Student Number Exist....");
                } else {
                    addStudent = true;
                    continue;
                }
             }
         }

         if (addStudent == true) {
             if (data[0].compareToIgnoreCase("C") == 0) {
                 Student_Course s1 = new Student_Course(
                                            data[1], data[2], 
                                            Long.parseLong(data[3])
                                        );
                 success = true;
                 studList.add(s1);
             } else if (data[0].compareToIgnoreCase("R") == 0) {
                 Student_Research s1 = new Student_Research(
                                                data[1], data[2], 
                                                Long.parseLong(data[3])
                                            );
                 success = true;
                 studList.add(s1);
             } else {
                 // If the enrolement is neither C or R, print the student ID and show the enrolement type
                 // Continue to add the rest
                 System.out.println();
                 System.out.println("Student Number: " + data[3] + " from file with invalid type - " + data[0]);
                 success = false;
                 continue;
             }
        }
    }
}
public class Student_Course extends Student {
    private Unit_Course unitCourse;
    private String enrolementType;

    public Student_Course(String fName, String lName, long studNo) {
        super(fName, lName, studNo);
        enrolementType = "C";
    }
}

the program should use the method to create objects instance of my student_course but i dont know where is the error

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • What line is the error on. The displayed exception stack-trace should indicate that. – DevilsHnd - 退職した Nov 03 '22 at 09:02
  • 1
    Debug your code and check `data`. Is it an array of length 4 as expected? Extract `String[] data = row.split(",");` as a method and write some tests. – Stefan Warminski Nov 03 '22 at 09:05
  • Some code improvements: change `studList.size() == 0` to `studList.isEmpty()`. `else if(studList.size() > 0)` is redundant because the size will always be > 0 if it is not empty – Stefan Warminski Nov 03 '22 at 09:13
  • One possible cause for such a problem would be an empty line at the end of the file, or basically any line without commas. – Thomas Kläger Nov 03 '22 at 09:37
  • Hi, the problem is solved. There are empty line at the end of the file. Lines without commas, if they anyway to prevent this since I alr have while(reader.hasNextLine()) it should stop the loop – Aloysious Kok Nov 03 '22 at 09:46
  • `String row = reader.nextLine(); row = row.trim(); if (row.isEmpty()) { continue; }` – DevilsHnd - 退職した Nov 03 '22 at 09:49
  • Also, your check to see if Student ID is already in the list should possibly go like this: `String[] data = row.split(","); addStudent = true; if (!studList.isEmpty()) { for (Student s : studList) { if (s.getStudNo() == Long.parseLong(data[3])) { addStudent = false; break; } } }` – DevilsHnd - 退職した Nov 03 '22 at 09:55
  • unrelated: stick to java naming conventions please (no underscores) – kleopatra Nov 03 '22 at 10:13
  • `reader.hasNextLine()` returns true as long as there are lines in the input. An empty line in the input is still a line. Imagine that you are reading a Java source file using `BufferedReader` and it would stop at the first empty line - that would be bad. – Thomas Kläger Nov 03 '22 at 12:10

0 Answers0