I have a text file of a list of Students that lists last name, first name, lab grade, project grade, and exam grade, for example:
Owens Will 46 54 56
Smith John 44 77 99
I am trying to write a method that reads the textfile, uses each line to create a Student object and then adds that to an ArrayList of Students. A Student object consists of the first name, last name, lab, project, and exam grades.
Here's what I have so far:
private ArrayList<Student> arraylist = new ArrayList<Student>();
public void ReadFile(String inputfile) throws FileNotFoundException {
File myFile = new File(inputfile);
Scanner sc = new Scanner(myFile);
while (sc.hasNextLine()) {
arraylist.add(sc.nextLine());
}
}
I'm unsure of how to create the Student object from the text file and then unsure of how to place the objects into the ArrayList?
EDIT:
Here's my Student class:
public class Student {
// fields
private String firstname;
private String lastname;
private int labgrade;
private int projectgrade;
private int examgrade;
private int totalgrade;
// constructor
public Student(String firstname, String lastname, int labgrade,
int projectgrade, int examgrade) {
this.firstname = firstname;
this.lastname = lastname;
this.labgrade = labgrade;
this.examgrade = examgrade;
this.totalgrade = labgrade + projectgrade + examgrade;
}
// method
public String toString() {
String s = firstname + " " + lastname + " has a total grade of " + totalgrade;
return s;
}
}
Studentclass?String#split