-1

I have the following Android program code. It does not work even I input "admin", the correct answer. Then I Toast my input which exactly the same as the == "admin". But it still does not work !!! Is there any trick ? Please help !! Many thanks !!

EditText etPassword =(EditText) findViewById(R.id.etPassword);
String t_etPassword =etPassword.getText().toString();

// display the t_etPassword for checking since the "if" does not work
Toast.makeText(getApplicationContext(), "<<" +t_etPassword +">>", Toast.LENGTH_SHORT).show();

if (t_etPassword == "admin") {
    Intent intent = new Intent(getBaseContext(), ContactListActivity.class);
    startActivity(intent);
   } else {
    Toast.makeText(getApplicationContext(), "Un-Authorised Access !!", Toast.LENGTH_SHORT).sho();
    }
  }
});
JoxTraex
  • 13,423
  • 6
  • 32
  • 45

4 Answers4

3

Try to use 'equals' instead of '=='.

if ("admin".equals(t_etPassword)) {

Explanation: How do I compare strings in Java?

Edit: Dont move the text to a string variable, it's unnecessary. Try this:

EditText etPassword =(EditText) findViewById(R.id.etPassword);
if ("yourpass".equals(etPassword)) {
    Intent intent = new Intent(getBaseContext(), ContactListActivity.class);
    startActivity(intent);
   } else {
    Toast.makeText(getApplicationContext(), "Un-Authorised Access !!", Toast.LENGTH_SHORT).sho();
    }
  }
});
Community
  • 1
  • 1
Dancger
  • 172
  • 3
  • 9
1
if (t_etPassword == "admin")

change above line to

if (t_etPassword.equals("admin"))

now you will get correct answer

Dinesh Raj
  • 664
  • 12
  • 30
0

Please change this line:

if (t_etPassword == "admin")

to

if (t_etPassword.equals("admin"))
user2339071
  • 4,254
  • 1
  • 27
  • 46
0

You should use .equals instead:

if(t_etPassword.equals("admin"))

== is checking for reference equality whereas .equals is used to compare values.

marteljn
  • 6,446
  • 3
  • 30
  • 43