-1

For a little android project I'm making a loginscreen. I have variables, but they stay empty once I fill them in and press the button.

these are my variables:

private EditText email;
private EditText wachtwoord;
private Button loginButton;

here I fill them:

        email = (EditText)findViewById(R.id.emailInput);
        wachtwoord = (EditText)findViewById(R.id.wachtwoordInput);
        loginButton = (Button)findViewById(R.id.loginButton);
        
        }
}

and my method which always executes the catch code

    public void login(View view)
    {
        
        try {
        
        if(email.getText().toString() == "jolt_koens@hotmail.com" && wachtwoord.getText().toString() == "admin")
        {
            Toast.makeText(getApplicationContext(), "gelukt", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "mislukt", Toast.LENGTH_SHORT).show();
            loginButton.setEnabled(false);
        }
        
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Jopie95
  • 3
  • 3
  • 7
    use `.equals()` to compare strings `email.getText().toString().equals("jolt_koens@hotmail.com")` – Raghunandan May 08 '14 at 08:06
  • I am bit confused why these guy's giving the answer one after other, when raghunandan is already written it in comment, And strange answer's is same as comment. – Robi Kumar Tomar May 08 '14 at 08:24

4 Answers4

0

Compare strings using either of these methods

method 1:

 if(((email.getText().toString().compareTo("jolt_koens@hotmail.com")==0) && (wachtwoord.getText().toString().compareTo("admin")==0))
 {
   do something
  }

method 2:

if(((email.getText().toString().equals("jolt_koens@hotmail.com") )&& (wachtwoord.getText().toString().equals("admin")))
    {
   do something
   }
Maxwell
  • 552
  • 2
  • 5
  • 20
0
public void login(View view)
    {

        try {

        if(email.getText().toString() .equals ("jolt_koens@hotmail.com") && wachtwoord.getText().toString().equals( "admin"))
        {
            Toast.makeText(getApplicationContext(), "gelukt", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "mislukt", Toast.LENGTH_SHORT).show();
            loginButton.setEnabled(false);
        }

Try the above code. Use .equals() instead of == To understand the difference between the two readthis.

Community
  • 1
  • 1
kgandroid
  • 5,507
  • 5
  • 39
  • 69
0

Change this line:

if(email.getText().toString() == "jolt_koens@hotmail.com" && wachtwoord.getText().toString() == "admin")

to this:

if(email.getText().toString().equals("jolt_koens@hotmail.com") && wachtwoord.getText().toString().equals("admin"))
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
0
if(email.getText().toString().equalsIgnoreCase("jolt_koens@hotmail.com") && wachtwoord.getText().toString().equalsIgnoreCase ("admin"))
        {
            Toast.makeText(getApplicationContext(), "gelukt", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "mislukt", Toast.LENGTH_SHORT).show();
            loginButton.setEnabled(false);
        }