0

My task is to extract all the records from a text file (which has 2 columns-name and manager name- and 9 rows), create an Employee object from each record ( name and manager), and then add the employee object to an ArrayList that accepts Employee objects.

For some reason, when I attempt to loop through the list later on, it seems as if the last record is what is being stored for all of the records.

Below is my code. I even put some print line statements to make sure that token was the correct name and manager name right before they get added, but again, when I loop through it afterwards all the records show up as "Veronica and Bob".

Here is the text file:

Betty     Sam
Bob       Sally
Dilbert   Nathan
Joseph    Sally
Nathan    Veronica
Sally   Veronica
Sam Joseph
Susan   Bob
Veronica

Here is my method:

List<Employee> employees = new ArrayList();

 public void loadData() throws FileNotFoundException, IOException {
    FileReader fr = new FileReader("C:\\Users\\jeff\\Documents\\NetBeansProjects\\Java3Project1\\src\\java3project1\\employee.txt");
    BufferedReader br = new BufferedReader(fr);
    String line;
    Employee e = new Employee();
    while ((line = br.readLine()) != null) {

        StringTokenizer st = new StringTokenizer(line);
        int counter = 0;
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (counter==0){
                e.setName(token);
                System.out.println("Name added: " + token);

            }
            if (counter == 1) {

                    e.setManager(token);
                    System.out.println("Manager set: " + token);
            }   
            employees.add(e);
            counter++;
        }//end while2
    }//end while1
} //end loadData()

When I check the list after this method was called, all the records come out as Name: Veronica Manager: Bob

I cant seem to figure out what I am doing wrong. All help is greatly appreciated. Let me know if you have any questions or need more clarification.

Thank you!

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • You code leaks resources, because it does not close the input streams. – Raedwald Feb 14 '16 at 18:30
  • Possible duplicate of [Why does my ArrayList contain N copies of the last item added to the list?](http://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list) – Raedwald Feb 14 '16 at 18:32

2 Answers2

2

All the employees in your arraylist refer to the same e instance that you create right before you start reading from the file, (They all share the same instance).

You need to change

Employee e = new Employee();
while ((line = br.readLine()) != null) {

to

while ((line = br.readLine()) != null) {
    Employee e = new Employee();

in order to create a new, separate instance for each Employee, before adding it to the arraylist.

EDIT:

You were adding a new employee at the end of each iteration of your inner loop (which runs twice), & that's why you were getting the records doubled.

while ((line = br.readLine()) != null) {
    Employee e = new Employee();
    StringTokenizer st = new StringTokenizer(line);
    int counter = 0;
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        if (counter==0){
            // Set the name
        }
        if (counter == 1) {
            // Set the manager
        }

        // employees.add(e); // Move this line from here
        counter++;
    }//end while2
    employees.add(e); // to here : the name & the manager are now both set
}//end while1
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • Thanks for the advice! I attempted this, but now its printing the names twice. so the first two records in the ArrayList are coming up as Betty and Sam, the third and fourth record shows bob and sally, and so on... any ideas as to why its doing this? – user3105072 Feb 14 '16 at 17:50
1

You are only creating an Employee object one time, at this line:

Employee e = new Employee();

Then you are just assigning a new name to that Employee object over and over, so in the end that object has the name of the last employee in your file. You need to be creating a new Employee object for each Employee you read in.

Mark B
  • 183,023
  • 24
  • 297
  • 295