I have a problem with this code. what I'm trying to do is read the file and save the student identifier and score in the points array in the points property of the student object, but I continue to get the last points only when printing. Here is the code. can you tell me if the setter property is the correct way to assign an array in the student class? the problem is that the last line of the grading file is stored in each array, even if during debugging I see the transmitted array of points, and the studentID array works fine.
import lab6.*;
public class Main
{
public static void main(String[] args)
{
Student lab6 [] = new Student[40];
lab6 = Util.readFile("studentScores.txt", lab6);
lab6[4].printStudent();
}
}
Student class ------------------------------------
package lab6;
public class Student
{
private int SID;
private int scores[] = new int[5];
public int getSID()
{
return SID;
}
public void setSID(int SID)
{
this.SID = SID;
}
public int[] getScores()
{
return scores;
}
public void setScores(int scores[])
{
this.scores = scores;
}
public void printStudent()
{
System.out.print(SID);
System.out.printf("\t");
for(int i = 0; i < scores.length; i++)
{
System.out.printf("%d\t", scores[i]);
}
}
}
the util class --------------------------------------------------------------------
import java.io.*;
import java.util.StringTokenizer;
public class Util
{
public static Student [] readFile(String filename, Student [] stu)
{
try {
String line[] = new String[40];
StringTokenizer stringToken;
int studentID;
int[] studentScoreArray = new int[5];
FileReader file = new FileReader(filename);
BufferedReader buff = new BufferedReader(file);
boolean eof = false;
int i = 0;
buff.readLine();
while (!eof)
{
line[i] = buff.readLine();
if (line[i] == null)
eof = true;
else
{
stringToken = new StringTokenizer(line[i]);
String tokenID = stringToken.nextToken().toString();
studentID = Integer.parseInt(tokenID);
stu[i] = new Student();
stu[i].setSID(studentID);
int quizNumberCounter = 0;
while (stringToken.hasMoreTokens())
{
String tokens = stringToken.nextToken().toString();
studentScoreArray[quizNumberCounter] = Integer.parseInt(tokens);
quizNumberCounter++;
}
stu[i].setScores(studentScoreArray);
}
i++;
}
buff.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
return stu;
}
}
Example file structure -------------------------------------------- --- --------
4532 011 017 081 032 077
source
share