9

Im trying to compare the values of two edittext boxes. What i would like is to just compare passw1 = passw2. As my code is now comparing two strings i have entered as i could not get to compare them.

 final EditText passw1= (EditText) findViewById(R.id.passw1);
 final EditText passw2= (EditText) findViewById(R.id.passw2);
 Button buttoks = (Button) findViewById(R.id.Ok);
      buttoks.setOnClickListener(new OnClickListener() {    

    public void onClick(View v) {       

     if (passw1.toString().equalsIgnoreCase("1234") && passw2.toString().equalsIgnoreCase("1234")){
      Toast.makeText(getApplication(),"Username and password match", Toast.LENGTH_SHORT).show();
      }
    else {
        Toast.makeText(getApplication(),"Username and password doesn't match", Toast.LENGTH_SHORT).show();
      }
     }   }); 
Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121
M2Gd
  • 225
  • 2
  • 3
  • 6

9 Answers9

35

Using the == operator will compare the references to the strings not the string themselves.

Ok, you have to toString() the Editable. I loaded up some of the code I had before that dealt with this situation.

String passwd1Text = passw1.getText().toString();
String passwd2Text = passw2.getText().toString();

if (passwd1Text.equals(passwd2Text))
{
}
Andrew T Finnell
  • 13,417
  • 3
  • 33
  • 49
17

[EDIT] I made a mistake earlier, because, to get the text, you need to use .getText().toString().

Here is a full working example:

package com.psegina.passwordTest01;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener {
    LinearLayout l;
    EditText user;
    EditText pwd;
    Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        l = new LinearLayout(this);
        user = new EditText(this);
        pwd = new EditText(this);
        btn = new Button(this);

        l.addView(user);
        l.addView(pwd);
        l.addView(btn);
        btn.setOnClickListener(this);

        setContentView(l);
    }

    public void onClick(View v){
        String u = user.getText().toString();
        String p = pwd.getText().toString();
        if( u.equals( p ) )
            Toast.makeText(getApplicationContext(), "Matches", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(getApplicationContext(), user.getText()+" != "+pwd.getText(), Toast.LENGTH_SHORT).show();
    }
}

Original answer (Will not work because of the lack of toString())

Try using .getText() instead of .toString().

if( passw1.getText() == passw2.getText() )
#do something

.toString() returns a String representation of the whole object, meaning it won't return the text you entered in the field (see for yourself by adding a Toast which will show the output of .toString())

mrPjer
  • 383
  • 3
  • 6
  • i tried this but all i get is they do not match even when they are – M2Gd Jan 15 '11 at 15:49
  • Can you post the code block with getText() and the output of Log.d("test", passw1.getText()) and Log.d("test", passw2.getText())? – mrPjer Jan 15 '11 at 15:52
  • Also, you can try the following: if( (passw1.getText()).equals(passw2.getText()) ) – mrPjer Jan 15 '11 at 15:53
  • public void onClick(View v) { if( passw1.getText() == passw2.getText() ){ Toast.makeText(getApplication(),"Username and password match", Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(getApplication(),"Username and password doesn't match", Toast.LENGTH_SHORT).show(); }} – M2Gd Jan 15 '11 at 15:56
  • I edited the answer with a full working example that should do what you are asking for. – mrPjer Jan 15 '11 at 16:33
  • @mrPer I edited the answer because there was a typo in the last example, using == instead of equals(). – Flávio Etrusco Mar 12 '21 at 16:13
6

In onclik function replace first line with this line u will definitely get right result.

if (passw1.getText().toString().equalsIgnoreCase("1234") && passw2.getText().toString().equalsIgnoreCase("1234")){

3

You can compare the values using equals() of Java :

public void onClick(View v) {
    // TODO Auto-generated method stub

    s1=text1.getText().toString();
    s2=text2.getText().toString();

    if(s1.equals(s2))
        Show.setText("Are Equal");
    else
        Show.setText("Not Equal");
}
Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
2

You need both getText() - which returns an Editable and toString() - to convert that to a String for matching. So instead of: passw1.toString().equalsIgnoreCase("1234") you need passw1.getText().toString().equalsIgnoreCase("1234").

Highland Mark
  • 1,002
  • 1
  • 7
  • 12
1

ou can use String.compareTo(String) that returns an integer that's negative (<), zero(=) or positive(>).

Use it so:

You can use String.compareTo(String) that returns an integer that's negative (<), zero(=) or positive(>).

Use it so:

  String a="myWord";
  if(a.compareTo(another_string) <0){
    //a is strictly < to another_string
  }
  else if (a.compareTo(another_string) == 0){
    //a equals to another_string
  }
else{
  // a is strictly > than another_string
}    
Sathish
  • 1,455
  • 1
  • 16
  • 22
1

Try to use .trim() first, before .equals(), or create a new String var that has these things.

Jave
  • 31,598
  • 14
  • 77
  • 90
raphaelle
  • 11
  • 4
0

did the same here needed to show "success" twice response is data from PHP

 String res=response.toString().trim;

                            Toast.makeText(sign_in.this,res,Toast.LENGTH_SHORT).show();
    if ( res.compareTo("success")==0){
    Toast.makeText(this,res,Toast.LENGTH_SHORT).show();
    }
A_rmas
  • 784
  • 2
  • 10
  • 26
0
String password=passw1.trim();
if (password.equalsIgnoreCase("1234")){
      Toast.makeText(getApplication(),"Username and password match", Toast.LENGTH_SHORT).show();
}

You need to use the trim method in the String

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
joey
  • 1