1

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;
}
0

2 Answers 2

1

In this method:

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++;
    }
}

you assign the 1st newStudent object to all the items of the array.
So when you try to assign the 2nd or 3d, none of the items is null and no assignment is done.
Just stop the loop after you make the 1st assignment:

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;
            break;
        }
        i++;
    }
}

Edit:
your ClassRoster class's as it is would return only the last student's details.
But you should also check for nulls.
So change to this:

public String toString(){
    String classString = "";
    for(Student student : roster){
        if (student != null)
          classString += student.toString() + "\n";
    }

    return classString;
}

I don't know your Student class's toString(), I assume it's working as expected.

Sign up to request clarification or add additional context in comments.

1 Comment

Adding break to the while loop seemed to have broken my toString method for the ClassRoster class. It gives me a NullPointerException when I try to use the toString method. I edited the post and added the toString method.
1

Your while loop fills all available positions with the first element. Then, since no positions are empty, nothing is inserted.

The loop can be simply modified as:

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;
            break;
        }
        i++;
    }
}

Now, the program will come out of the loop once an empty position had been filled.

6 Comments

Adding break to the while loop seemed to have broken my toString method for the ClassRoster class. It gives me a NullPointerException when I try to use the toString method. I edited the post and added the toString method.
Is it the toString method that gives the exception, or the method that uses toString ?
It seems to be both (I think). 'Exception in thread "main" java.lang.NullPointerException at ClassRoster.toString; at GradeOrganizer.main
Can you provide the function where toString is called?
I added it on the main post, should be inside the while loop, under firstRoster.add(newStudent).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.