I am having troubles to transfer the data from an array to an ArrayList. I am getting the error: The method add(Course, Object[], int) in the type ArrayList is not applicable for the arguments (String, String, int, int, String, int, String). I already checked the data fields in the class Course, they are all correct.
Please help me!
ArrayList<Course> coursesList = new ArrayList<Course>();
boolean exit = false;
String fileName = "MyUniversityCoursesFile.csv";
String line = null;
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
br.readLine();
while ((line = br.readLine()) != null) {
String[] coursesArray = line.split(",");
String courseName = coursesArray[0];
String courseID = coursesArray[1];
String maxStudents = coursesArray[2];
String registeredStudents = coursesArray[3];
// String studentsList = coursesArray[4];
String instructor = coursesArray[5];
String sectionNumber = coursesArray[6];
String location = coursesArray[7];
coursesList.add(courseName, courseID, Integer.parseInt(maxStudents), Integer.parseInt(registeredStudents), instructor, Integer.parseInt(sectionNumber), location);
}
CodePudding user response:
You have created the ArrayList with Course Object, so when adding into the List, you need to create the Course object and then add it.
Course newCourse = new Course(courseName, courseID, Integer.parseInt(maxStudents), Integer.parseInt(registeredStudents), instructor, Integer.parseInt(sectionNumber), location);
coursesList.add(newCourse);
Also, it good practise to code to an interface, so instead of doing
ArrayList<Course> coursesList = new ArrayList<Course>();
Change it to,
List<Course> coursesList = new ArrayList<>();