0

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;
}
6
  • 3
    Can you post the code for your User class? What are the values of username and password each iteration of the loop? Commented Mar 4, 2014 at 18:26
  • 13
    Are the username and password fields static by any chance? Commented Mar 4, 2014 at 18:26
  • 1
    Storing username and password combinations in plain text (e.g. in your user_pass.txt file) is NEVER a good idea. You should correct that problem first. Commented Mar 4, 2014 at 18:29
  • When I print the values of username and password at each iteration they are fine (i.e. the file reading and string splitting is working properly). Commented Mar 4, 2014 at 18:30
  • 2
    @ErstwhileIII - Who said they are plain text? The password in the file could just as well be a SHA hash, or some such. Commented Mar 4, 2014 at 18:31

3 Answers 3

7

The username and password fields are static which means that they will retain a single value per field (namely the last assigned value) associated with the class. Removing the static keyword will make them instance variables and create a value per instance of the class

private String username;
private String password;
Sign up to request clarification or add additional context in comments.

Comments

0

The username and password fields in the User class are static, meaning they are global class variables (called using User.username and User.password). You should remove the static keyword so that individual User object instances can have a username and a password.

Comments

0

Delete the "static" in class User before the variables username and password, and you'll probably be ok.

Or, this should work:

class User {
    String username, password;

    public User(String u, String p) {
        username = u;
        password = p;
    }
}


//Code in the other class
Scanner s = new Scanner(new File("user_pass.txt"));
while (scanner.hasNext()) userList.add(new User(s.next(), s.next()));
s.close();

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.