Here is the method
public void addModuleToStudent(Module aModule)
{
int position = 0;
if(position > 3)
{
System.out.println("Error: Student already has four modules\n");
}
else
{
moduleArray[position] = aModule;
position++;
}
}
The problem is that the position doesn’t seem to be increment the position variable because when ever I add a module class it occupies the 1st position in the the array and when I add another instead of being added into the second position it overwrites the first.
Yes,
positionis a local variable. Every time you calladdModuleToStudent, you get a new variable, initialized as 0.It sounds like you want to make this an instance variable instead, so that it persists between method calls.
Better yet, don’t use an array instead – use an
ArrayList<Module>instead, and then you can just use: