-1

I can't seem to get the code within the second if statement to execute. I have logged both values being compared and also ran the debugger and checked them. They are both "a". It always shows the incorrectPasswordDialog. This question seems difficult because it seems as though it should just work but any help is appreciated.

private void logUserIn(AppUser user) {
            if (user != null){
                Log.d("mPassword: ", mPassword);
                Log.d("user.getPassword(): ", user.getPassword());
                String userPassword = user.getPassword().toString();
                String formPassword = mPassword.toString();
                if ( userPassword == formPassword ){
                    Intent welcomePage = new Intent(this, StartScreenActivity.class);
                    welcomePage.putExtra("name", mName);
                    startActivity(welcomePage);
                }
                else {
                    showIncorrectPasswordDialog();
                }
            }else {
                showIncorrectUserNameDialog();
            }
        }
user2864740
  • 60,010
  • 15
  • 145
  • 220
user2602079
  • 1,283
  • 5
  • 20
  • 39

6 Answers6

1

You are comparing the object identity. Use string.equals() to check for equivalence.

if(userPassword.equals(formPassword)){
}
edtheprogrammerguy
  • 5,957
  • 6
  • 28
  • 47
0

Change

if ( userPassword == formPassword ){

to

if ( userPassword.equals(formPassword) ){

== compares object references, whereas .equals compared String values.

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
0

You can not compare string using == try like this

Compares the specified object to this string and returns true if they are equal. The object must be an instance of string with the same characters in the same order.

 if ( userPassword.equals(formPassword)){
    // do some stuff
  }

or

Compares the specified string to this string ignoring the case of the characters and returns true if they are equal.

 if(userPassword.equalsIgnoreCase(formPassword))
 {
     //do some stuff
  }
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
0

In JAVA to compare between strings, you should use:

if(userPassword.equals(formPassword)) {
   // they are equal
}
Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
0

Change

if ( userPassword == formPassword ){

to

if ( userPassword.equals(formPassword)){

In Java, String comparison is done with .equals(). Using == compares the object reference and not their values.

Java String comparison

codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

In JAVA you should use equals to judge equal of value of different String. == is used to judge object pointer for string, so it would only return false in your case.

Wayne
  • 1