1

private void validate(String userName, String userPassword){
        if((userName == "Admin") && (userPassword == "1234")){
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);
        } else{
            counter --;
            Info.setText(userName + " Number of Attempts Left: "+String.valueOf(counter) + " - " + userPassword);
            if(counter == 0){
                Login.setEnabled(false);
            }
        }
    }

Whenever i give input as "Admin" and "1234" condition goes to else part. please help me i am beginner in this field.

Mohit
  • 1,204
  • 2
  • 13
  • 19

3 Answers3

1

string equals() method compares the two given strings based on the data/content of the string.

Try with

  if(userName.equals("Admin") && userPassword.equals("1234")){
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);
        } else{
            counter --;
            Info.setText(userName + " Number of Attempts Left: "+String.valueOf(counter) + " - " + userPassword);
            if(counter == 0){
                Login.setEnabled(false);
            }
        }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

You should compare strings using equals method:

"Admin".equals(UserName)

BTW, the code above is a bit safer than

UserName.equals("Admin")

because it does not throw NullPointerException when UserName is null.

Alex Sveshnikov
  • 4,214
  • 1
  • 10
  • 26
0

== tests for reference equality (whether they are the same object).

.equals() tests for value equality (whether they are logically "equal").

Therefore use .equals(), or equalsIgnoreCase() instead of == for comparing String objects.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
Deepak Tatyaji Ahire
  • 4,883
  • 2
  • 13
  • 35