0

I just want to check if the given passwords are not equal to each other. So I had made this:

    String password = etPassword.getText().toString().trim();
    String confirmPassword = etConfirmPassword.getText().toString().trim();

    if (password != confirmPassword) {
        Toast.makeText(getApplicationContext(),
                "Passwords do not match", Toast.LENGTH_LONG)
                .show();
    }

But somehow it doesn't work. I have logged it when I gave an input value "e" for both passwords.

superkytoz
  • 1,267
  • 4
  • 23
  • 43

2 Answers2

3

use this equals() method instead of != like this:

if (!password.equals(confirmPassword)) {

With Strings you almost always want to use the equals method. In fact with Objects in general you do.

Josh Chappelle
  • 1,558
  • 2
  • 15
  • 37
2

You cannot use != , try to compare with .equals()

if (password.equals(confirmPassword) == false) {
        Toast.makeText(getApplicationContext(),
                "Passwords do not match", Toast.LENGTH_LONG)
                .show();
    }
Ali Hasanzade
  • 299
  • 1
  • 3
  • 15