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

  • SEARCH
  • Home
  • 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 8991825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:49:01+00:00 2026-06-15T22:49:01+00:00

I was struggling with part of my code today to read some data from

  • 0

I was struggling with part of my code today to read some data from a file and add them to an object as its properties (I’m aware of how to add/read object in file without this hassle but wanted to do it this way) as below:

file is like this:

111,john,23.1
222,jack,22.5
234,adam,12.8

I was trying to read this file using following:

public ArrayList<Staff> LoadAllStaffs(){
    ArrayList<Staff> staffs = new ArrayList<Staff>();
    File file = new File(stafffile);
    Staff tmpstaff = new Staff();
    try {
        BufferedReader inputfile = new BufferedReader(new FileReader(stafffile));
        String tmp;
        while((tmp = inputfile.readLine()) != null){
            StringTokenizer st = new StringTokenizer(tmp , ",");
            tmpstaff.setID(Integer.valueOf(st.nextToken()));
            tmpstaff.setFirstName(st.nextToken());
            tmpstaff.setSalary(Double.valueOf(st.nextToken()));
            staffs.add(tmpstaff);
            }
        } 
    catch (IOException e) {
    }
    return staffs;
}

using println this output shown from the returned ArrayList:

234,adam,12.8
234,adam,12.8
234,adam,12.8

I just moved the Staff tmpstaff = new Staff(); inside the while loop and it shows what it should.

why this is happening? I read -even here- that defining variables (well, here its an Object ) inside or outside loops doesn’t make any difference.

  • 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-06-15T22:49:02+00:00Added an answer on June 15, 2026 at 10:49 pm

    You are not creating a new Staff instance inside the loop, you are reusing the same instance for all the iterations. So, you overwrite the values and add the same object.

    Move

    Staff tmpstaff = new Staff();
    

    to the first line inside the loop.

    UPDATE: To address Vash‘s comment, the issue here is that can be explained as that, if you want to store 3 objects, you need to create such 3 objects. You can reuse the reference (the tmpstaff variable) and effectively where it is defined it is not important (as long as all references to it are in the same scope. But you must create the 3 objects, which means 3 new commands.

    UPDATE 2: To put things simpler, the text I read -even here- that defining variables (well, here its an Object ) inside or outside loops doesn't make any difference. means that

    File file = new File(stafffile);
    Staff tmpstaff = null; // or simpler, Staff tmpstaff;
    try {
       ...
       while((tmp = inputfile.readLine()) != null){
         tmpstaff = new Staff();
         ...
    

    and

       while((tmp = inputfile.readLine()) != null){
         Staff tmpstaff = new Staff();
         ...
    

    are equivalent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been struggling for a while with a part of my code and I
I have an object that has a multi-part key and I am struggling to
This code is from an Access 2007 project I've been struggling with. The actual
I am struggling to append data retreived from an SQL query in PHP. Basically
im struggling with this part of code , no matter what i try i
I'm struggling with understanding a piece of code that's part of a larger problem
I'm struggling to find an addon/plugin for this and its a key part of
I'm struggling with some JavaScript code that I have used countless times across my
I am struggling with the last part of my project in regards to HtmlUnit.
Hello I am new to assembly and I'm struggling to get a two-part program

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.