Java array, I get the last array all the time

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.*;//importing the necessary classes

public class Main 
{
    public static void main(String[] args) 
    {
        Student lab6 [] = new Student[40];
        //Populate the student array
        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];
      //write public get and set methods for SID and scores
    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;
    }
    //add methods to print values of instance variables.
     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;

//Reads the file and builds student array.
//Open the file using FileReader Object.
//In a loop read a line using readLine method.
//Tokenize each line using StringTokenizer Object
//Each token is converted from String to Integer using parseInt method
//Value is then saved in the right property of Student Object.
public class Util 
{
    public static Student [] readFile(String filename, Student [] stu)
    {
        try {
            String line[] = new String[40];//one line of the file to be stored in here
            StringTokenizer stringToken;
            int studentID;//for storing the student id
            int[] studentScoreArray = new int[5];//for storing the student score

            FileReader file = new FileReader(filename);
            BufferedReader buff = new BufferedReader(file);

            boolean eof = false;
            int i = 0;
            buff.readLine();//used this to skip the first line
            while (!eof) //operation of one line
            { 
                line[i] = buff.readLine();
                if (line[i] == null)
                    eof = true;
                else //tokenize and store
                {
                    stringToken = new StringTokenizer(line[i]);
                    String tokenID = stringToken.nextToken().toString();//for storing the student id
                    studentID = Integer.parseInt(tokenID);
                    stu[i] = new Student();//creating student objects
                    stu[i].setSID(studentID);//stored in student object
                    //now storing the score-------------------------------------------------
                    int quizNumberCounter = 0;
                    while (stringToken.hasMoreTokens()) 
                    {
                        String tokens = stringToken.nextToken().toString();
                        studentScoreArray[quizNumberCounter] = Integer.parseInt(tokens);//converting and storing the scores in an array
                        quizNumberCounter++;//array progression

                    }
                    stu[i].setScores(studentScoreArray);//setting the score(passing it as an array)
                    //-----------------------------------------------------------------------

                }
                i++;

            }
            buff.close();
        } catch (IOException e) {
            System.out.println("Error -- " + e.toString());
        }

        return stu;
    }
/*
     StringTokenizer st = new StringTokenizer("this is a test");
     while (st.hasMoreTokens()) {
         System.out.println(st.nextToken());
     }
//How to convert a String to an Integer
    int x = Integer.parseInt(String) ;*/
}

Example file structure -------------------------------------------- --- --------

4532 011 017 081 032 077 
+3
source share
3 answers

, :

int[] studentScoreArray = new int[5];

. , , .

:

int[] studentScoreArray = new int[5];
int quizNumberCounter = 0;
while(..) { ...}
0

int[] studentScoreArray = new int[5];

. (.. ) , .

// int[] studentScoreArray = new int[5]; // <= line removed here!!!

...

while (!eof) //operation of one line
{ 
    line[i] = buff.readLine();
    if (line[i] == null)
        eof = true;
    else //tokenize and store
    {
        int[] studentScoreArray = new int[5]; // <= line moved over to here!!!

        ...
    }
}
+2

, , , :

lab6[4].printStudent();

, :

foreach (Student student : lab6)
{
    student.printStudent();
}

, , students lab6. java Type[] identifier, Type identifier [].

: , - , !

0
source

All Articles