0

I am trying to check user's inserted password with saved password in the database.
This is my methode:

private Boolean CheckPassword()
        {
            TextView txt_pass = (TextView) findViewById(R.id.login_password_txt);
            String _writtenPassword = txt_pass.getText().toString().trim();

            if (_writtenPassword == userPassword)
                return true;


            Log.d(TAG, userPassword);
            Log.d(TAG, _writtenPassword);
            Log.d(TAG, String.valueOf(userPassword.length()));
            Log.d(TAG, String.valueOf(_writtenPassword.length()));

            return false;
        }

The log shows me the password and length.
The result is:

123
123
3
3

But this methode returns False!!!

Can anyone help me please?

  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – laalto Apr 16 '14 at 12:20

4 Answers4

4

Try this..

String you should compares with .equals("")

if (_writtenPassword.equals(userPassword))
            return true;

== always just compares two references

Refer this String comparison

Community
  • 1
  • 1
Hariharan
  • 28,756
  • 7
  • 51
  • 55
0

Use the if-else control structure and also use the equals() for comparing two strings like this,

if (_writtenPassword.equals(userPassword))
     return true;
else
     return false;
Balayesu Chilakalapudi
  • 1,368
  • 3
  • 19
  • 43
0

Alternatively you can use matches

if (_writtenPassword.matches(userPassword))
                return true;
///your code
Siva
  • 9,159
  • 10
  • 36
  • 59
0

expression _writtenPassword == userPassword is true whenever reference of _writtenPassword and userPassword are same.

Since you don't care about their reference but you care about their value so use expression _writtenPassword.equals(userPassword)which is true whenever the value of_writtenPasswordanduserPassword` are same.

Try this:

return _writtenPassword.equals(userPassword);
Hamid Shatu
  • 9,503
  • 4
  • 29
  • 40
Shahidul
  • 2,757
  • 1
  • 20
  • 20