Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5991473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:23:29+00:00 2026-05-22T23:23:29+00:00

I am having a problem in this code. what i am trying to do

  • 0

I am having a problem in this code. what i am trying to do is read a file and store a studentID and score into an array of scores into the scores property of a student object, but I keep getting the last scores only when I print. Here is the code. can you tell me if my setter property is a correct way of assigning an array in the student class? the problem is the last line of the score file is stored in every array even though when I debug it I see the score array being passed 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();         
    }

}

The 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) ;*/
}

Sample file Structure ——————————————————-

4532 011 017 081 032 077 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:23:30+00:00Added an answer on May 22, 2026 at 11:23 pm

    I havent tested the code with my suggestion, but take a look at:

    int[] studentScoreArray = new int[5];
    

    You create this once and once only for the whole file.
    A simple and easy fix is to do it for every new line read instead.

    like this :

    int[] studentScoreArray = new int[5];
    int quizNumberCounter = 0;
    while(..) { ...}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to convert this test code to C# and having a problem
I am having some trouble with this code . The problem is when i
I have been trying to tackle this problem , but I am having difficulty
I'm trying to create a simple class to read a csv file and store
Im trying to read in image file from a server , with the code
I am having a problem trying to sort with an XSL file using the
I'm having this problem, same as ever, but never try to find the right
I'm having this problem and I reached a deadlock, I would try anything I've
I am having virtually the same problem as this: C# Update combobox bound to
I am having a problem with links wrapping. How do I prevent this? ![Wordwrap][1]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.