I'm trying to add a list of username and password combinations from a text file (each line contains a username password pair separated by a whitespace) into an ArrayList called userList. The User constructor takes two strings a sets them as username and password. This should be simple, but for some reason when I traverse the array after the following code, all of the users have the same username and password (the last line in the text file).
See code below:
BufferedReader br = new BufferedReader(new FileReader("user_pass.txt"))
String line, username, password;
while ((line = br.readLine()) != null) {
String separated[] = line.split(" ");
username = separated[0];
password = separated[1];
userList.add(new User(username,password));
}
Here's how I'm traversing the arrayList:
for(User u : userList){
System.out.println(u.getUsername()+" --> "+u.getPassword());
}
And here's the user class:
public class User{
private static String username;
private static String password;
public User(String u, String p) {
username = u;
password = p;
}
public String getUsername(){
return username;
}
public String getPassword(){
return password;
}
usernameandpasswordfields static by any chance?