1

I am currently developing an app that will require that the user enters their password to access the game. I have the following if statement but it will not work, as you can see from the if statements I have tried three different ways to get the match to equal true.

    EditText passInput = (EditText) findViewById(R.id.passwordBox);
    CharSequence Password = passInput.getText();
    RelativeLayout loggedIn = (RelativeLayout) findViewById(R.id.LoggedInLayout);
    RelativeLayout CreateUser = (RelativeLayout) findViewById(R.id.createUserLayout);
    Button loginBtt = (Button) findViewById(R.id.createUser);
    String actualPass = password[x];

    System.out.println(Password + actualPass + passInput);

    if(Password.equals(actualPass)){

        System.out.println("They Matched!");
        loggedIn.setVisibility(View.VISIBLE);
        CreateUser.setVisibility(View.GONE);
        loginBtt.setText("Create User");

    }else if(Password.toString() == actualPass.toString()){

        System.out.println("Second Match");

    }else if(Password == actualPass){

        System.out.println("Third Match");

    }else if(Password.equals(actualPass) == false){

    System.out.println("Wrong");
    incorrectPassword();
    System.out.println(Password);
    System.out.println(actualPass);

    }

When the user registers they are required to set a password. For testing I tried the password 'trst' but when inserted to the login page it comes back as being not correct. This is what my LogCat displays:

11-07 11:46:16.357: I/System.out(1998): Wrong
11-07 11:46:16.547: I/System.out(1998): trst
11-07 11:46:16.547: I/System.out(1998): trst

As you can see from the LogCat the inserted password and the actual password are identical but the program says they are not!

Alex
  • 531
  • 1
  • 8
  • 22

4 Answers4

3

Use .equals() instead of == for checking if one String objects is equal to another. == returns true if two references are referencing the same object, while .equals() returns true if contents of two strings are identical.

aga
  • 27,954
  • 13
  • 86
  • 121
  • Thanks, I had tried this already but it appears that it wasn't working because I was trying to compare a String to a CharSequence – Alex Nov 07 '13 at 12:07
  • `String` is a class which implements `CharSequence` interface, so it doesn't matter. – aga Nov 07 '13 at 12:09
  • That's odd. It worked as I was wanting it to when I changed it from a CharSequence to a String – Alex Nov 07 '13 at 12:11
  • @Alex try to use `.equals()` insted of `==`, keep `CharSequence` and see if it works. I tried to check equality of `String` and `CharSequence` and it works. – aga Nov 07 '13 at 12:18
2

try Password.toString().equals(actualPass.toString())

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
2

You cannot not compare CharSequence with String using equal().So change CharSequence to String by using this

String Password = passInput.getText().toString();
krishna
  • 4,069
  • 2
  • 29
  • 56
1

Change

 CharSequence Password = passInput.getText();

To

 String Password = passInput.getText().toString();

then

if(Password.equalsIgnorecase(actualPass)){

        System.out.println("They Matched!");
        loggedIn.setVisibility(View.VISIBLE);
        CreateUser.setVisibility(View.GONE);
        loginBtt.setText("Create User");

    }else if(Password.equalsIgnorecase(actualPass.toString())){

        System.out.println("Second Match");

    }else if(Password.equalsIgnorecase(actualPass)){

        System.out.println("Third Match");

    }else if(!Password.equalsIgnorecase(actualPass)){

    System.out.println("Wrong");
    incorrectPassword();
    System.out.println(Password);
    System.out.println(actualPass);

    }
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44