androidobjectequals

How to compare objects to user input in an if statement


I'm trying to compare the users input (username and password) with the user objects (eg user1, user5) to see if they match and if they do then the user will be directed into another activity because they would've been able to successfully logged in, and if not a message will be displayed. Ive created a class called User which I call to get the userName and password.

I think a if statement will work for this to compare them by using .equal but when I try the app no matter if the input is correct or not it buts the message that the details were incorrect. Its like it is fully ignoring all my if statements apart from the last one.

Can anyone tell me if there is something wrong in my code?

Errors aren't showing up anywhere.

User class:

public class User {

// Instance variables
static String userName;
static String password;
static String favColor;


// User constructor
public User(String initUserName, String initPassword, String initFavColor) {
    userName = initUserName;
    password = initPassword;
    favColor = initFavColor;

}

// Getter method to return userName
public String getUserName(){
    return userName;
}

public String getPassword(){
    return password;
}

public String getFavColor(){
    return favColor;
}

Main Activity:

    logInBt.setOnClickListener(new View.OnClickListener() {
        public void  onClick(View view) {

            User user1 = new User("Jason", "Sword", "Red");
            User user2 = new User("Billy", "Dinosaur", "Blue");
            User user3 = new User("Zack", "Elephant", "Black");
            User user4 = new User("Trini", "Tiger", "Yellow");
            User user5 = new User("Kimberly", "Bird", "Pink");

            String userET = userEditText.getText().toString();
            String userPassword = passwordEditText.getText().toString();

            if(userET.equals(user1.getUserName()) & userPassword.equals(user1.getPassword())){

                Intent i = new Intent(getApplicationContext(), MainMenu.class);
                startActivity(i);

            } else if(userET.equals(user2.getUserName()) && userPassword.equals(user2.getPassword())){

                Intent i = new Intent(getApplicationContext(), MainMenu.class);
                startActivity(i);

            }else {
                Toast.makeText(getApplicationContext(), "Incorrect details. Please try again", Toast.LENGTH_SHORT).show();

            }



        }

});

Solution

  • The properties of an object must not be of the static type, the static value is kept while the application is in use, therefore, each time an instance of the object is created, it acquires a new value

    Solution:
    public class User {
    
    //Instance variables
    private String userName;
    private String password;
    private String favColor;
    
    
    // User constructor
    public User(String userName, String password, String favColor) {
        this.userName = userName;
        this.password = password;
        this.favColor = favColor;
    
    }
    
    // Getter method to return userName
    public String getUserName(){
        return userName;
    }
    
    public String getPassword(){
        return password;
    }
    
    public String getFavColor(){
        return favColor;
    }
    }
    

    I can recommend you read about static variables