I wrote a method for adding a Student object into a roster array.
void add(Student newStudent){
int i = 0;
while(i != classSize){ //classSize is the size of the roster array
if(roster[i] == null { //roster is an array of Student objects
roster[i] = newStudent;
}
i++;
}
}
The problem I'm having is that when I'm using this method in my main class, it seems to only add and print the first object.
The portion of my main method:
ClassRoster firstRoster = new ClassRoster();
scan = new Scanner(inputFile).useDelimiter(",|\\n");
while(scan.hasNext()){
String name = scan.next();
int gradeLevel = scan.nextInt();
int testGrade = scan.nextInt();
Student newStudent = new Student(name,gradeLevel,testGrade);
firstRoster.add(newStudent);
System.out.printf(firstRoster.toString());
}
The input text file would look something like this:
John,12,95
Mary,11,99
Bob,9,87
However, when I try printing the firstRoster array, it only prints the first object. In this case, it will print John 3 times.
John,12,95
John,12,95
John,12,95
If I add another student in the text file, it will just print John 4 times instead and so on.
toString method in the ClassRoster class:
public String toString(){
String classString = "";
for(Student student : roster){
classString = student.toString(); //The student object uses another toString method in the Student class
}
return classString;
}