My arraylist is returning the same value on all "nodes"

3

This code should take all the elements of a login.txt file and place it inside a user array.

 public ArrayList<User> takeAll(){
    ArrayList<User> list = new ArrayList();
        User u = new User();
            String result = "";
            try {
            FileReader fr = new FileReader("C:\Users\vanes\Documents\WEBprog\Archive\login.txt");
            BufferedReader br = new BufferedReader(fr);
            while (br.ready()) {
                result = br.readLine() + "\n";
                String divider[] = new String[2];//split                
                divider = result.split(";"); //split
                u.setName(divider[0]);
                u.setLogin(divider[1]);
                u.setPassword(divider[2]);
               // System.out.println(u.getName()+";"+u.getLogin()+";"+u.getPassword());
                list.add(u);
            }
            br.close();
            fr.close();
        } catch (Exception ex) {
            System.out.println("Erro");
        }
    for (int i = 0; i < list.size(); i++) 
        System.out.println(list.get(i).getLogin());
    return list;    
}

I see that it's being added the way it's meant to be. However, when I try to print before the return list, the result appears the same for all nodes. I wonder where the error is.

It seems like it is not writing correctly within the while. I've already tested the piece of code somewhere else and it works perfectly, that part of the for, actually, the error appears to be within the while, but I can not see.

    
asked by anonymous 05.09.2018 / 01:19

2 answers

1

The problem is that you only create a User out of the loop:

        User u = new User();

And inside you change the data, but always of the same User :

                u.setName(divider[0]);
                u.setLogin(divider[1]);
                u.setPassword(divider[2]);

Then, if you uncomment the print will get the impression that it worked:

  // System.out.println(u.getName()+";"+u.getLogin()+";"+u.getPassword());

But at the end, when you go through the list you will see that you have several references to the same User.

The solution is to create a inside loop and add it.

    
05.09.2018 / 01:27
0

Really missing a new every round in the While would look like below, should solve your problem:

public ArrayList<User> takeAll(){
    ArrayList<User> list = new ArrayList();
        User u = new User();
            String result = "";
            try {
            FileReader fr = new FileReader("C:\Users\vanes\Documents\WEBprog\Archive\login.txt");
            BufferedReader br = new BufferedReader(fr);
            while (br.ready()) {
                result = br.readLine() + "\n";
                String divider[] = new String[2];//split                
                divider = result.split(";"); //split
                u.setName(divider[0]);
                u.setLogin(divider[1]);
                u.setPassword(divider[2]);
               // System.out.println(u.getName()+";"+u.getLogin()+";"+u.getPassword());
                list.add(u);
                u = new User();
            }
            br.close();
            fr.close();
        } catch (Exception ex) {
            System.out.println("Erro");
        }
    for (User user : list) {
        System.out.println(user.getLogin());
    }

    return list;  
}
    
06.09.2018 / 23:02