imawarrior
imawarrior

Reputation: 33

Writing a method for adding an object to an array

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

Upvotes: 1

Views: 95

Answers (2)

forpas
forpas

Reputation: 164064

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.

Upvotes: 1

Susmit Agrawal
Susmit Agrawal

Reputation: 3764

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.

Upvotes: 1

Related Questions