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