Comparing strings in Java


Question

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();
      }
     }   }); 
1
9
1/15/2011 4:23:46 PM

Accepted Answer

[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())

17
1/15/2011 4:32:30 PM

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))
{
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon