Linked Questions

Popular Questions

How to see if password matches in arraylist of objects

Asked by At

I am trying to see if the password that was user inputted in method addUser() matches the password user inputted in the method deleteUser(). Both methods look into the users arraylist that is full of FacebookUser objects, However, I cannot figure out how to match the appropriate passwords from each method.

I have tried multiple codes I can hardly even remember, sometimes I'd always get "incorrect password" returned even when it is correct But whenever I had only one object in my arraylist it would work until I added another user. It would make both users end up with the same password.

public class Facebook extends FacebookUser implements Serializable {

    private static final long serialVersionUID = 1L;
    Scanner input = new Scanner(;
    private ArrayList<FacebookUser> users;

    public Facebook(String username, String password) {
        super(username, password);
        users = new ArrayList<>();


    // prints users
    public void listUsers() {
        if( users.isEmpty())
            System.out.println("List is empty");
            for (FacebookUser users : this.users)

            System.out.println("Username: " + users.getUsername());

    // adds a user

    public void addUser() {

        //creating an instance of FacebookUser class
        FacebookUser user1 = new FacebookUser();

        System.out.println("Enter username to add: ");
        String newUser = input.nextLine();
    // using the setter method in UserAccount to set the username for "fb" object

        //if statement checks if any object contains the same username  
        if (users.contains(user1)) { 
            System.out.println("Error, username already exists");

        else {

            System.out.println("Please enter password: ");
            String password = input.nextLine();
            System.out.println("Please enter password hint: ");
            String passwordHint = input.nextLine();

            FacebookUser user2 = new FacebookUser();
            //creating another object for the "official" new user



            System.out.println(newUser + " has been added");


    // deletes a user
    public void deleteUser() {

    FacebookUser user1= new FacebookUser();
    System.out.println("Enter username you want to remove: ");
    String removeUser = input.nextLine();   


        if (users.contains(user1)) {
            System.out.println("Enter password");
            String checkPassword = input.nextLine();

            if (user1.password.equals(checkPassword)) {
                System.out.println(removeUser + " has been removed");
            } else {
                System.out.println("Incorrect password");
        } else {
            System.out.println("User does not exist");


Related Questions