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.