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

Asked

Viewed 49 times

3

This code should take all the elements of a login.txt file and put 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 it being added the way it is 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 to me that he is not recording properly inside the while. I’ve tested the piece of code somewhere else and it works perfectly, that part of the for really, the error appears to be inside the while, but I can’t see.

  • Where is the print?

2 answers

1


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

        User u = new User();

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

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

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

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

But at the end, when it comes to "go over the list" you will see that there are several references to the same User.

The solution is to create a new User() within of loop and add.

0

Really missing a new each round on 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;  
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.