I have a Student class and in the main class I have a method which takes a fileName(the file contains student records with names and marks of subjects) as input and returns an array of Student as output.
I tried using ArrayList however I am stuck converting the student's record to student array. Please help..Below is my code:
public static void main(String[] args) throws FileNotFoundException {
String filename = "student.txt";
readData(filename);
}
private static Student[] readData(String filename) throws FileNotFoundException{
Scanner input = new Scanner(new File(filename));
ArrayList<Student> list = new ArrayList<Student>();
while(input.hasNext()){
String name = input.next();
int physic = input.nextInt();
int chemistry = input.nextInt();
int math = input.nextInt();
Student studentR = new Student(name,physic,chemistry,math);
list.add(studentR);
System.out.println(list);
}
for (Student studentR : list) {
System.out.println(studentR);
return studentR;
}
}
And my text file looks like:
Amy 85 95 78
Nancy 83 93 82
Richard 81 91 86
Daniel 77 78 79
James 80 90 85
Cathy 95 74 89
Paul 84 87 79
Below is the error I am getting:
Main.java:28: error: incompatible types: Student cannot be converted to Student[]
return studentR;
^