0

I have an android app. in that I am comparing two strings from the EditTest, but I get some strange results...

if(v==findViewById(R.id.submit)){
            //

            if(email==crfm_email)
            {

                String warn="Done!";
                Toast toast = Toast.makeText(getBaseContext(), warn, Toast.LENGTH_LONG);
                toast.show();
            }
            else
            {
                oFN=(EditText)findViewById(R.id.owners_first_name);
                String warn="email addresses do not match "+eMail+" "+crfEmail;
                Toast toast = Toast.makeText(getBaseContext(), warn, Toast.LENGTH_LONG);
                toast.show();

            }

        } 

the problem is that the control always goes to the else part even if the input for both variables are same. I know that its a simple mistake but cant crack it out...!

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
rahul
  • 2,758
  • 5
  • 32
  • 53
  • thanks why is this comment" "if" is not a loop.!!!! – radkrish"? – rahul Feb 04 '11 at 13:25
  • Iam confused..because I have more than one answer for this question and all are similar..which one to be accepted..the first responce..? – rahul Feb 04 '11 at 13:26

4 Answers4

3

Use:

if (email.equals(crfm_email))
{
    ....

to compare the strings. The point being you want to compare the contents of the Strings, not the references to the String objects.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
2

Looks like you are doing an object ref compare instead of an object compare.

if(email==crfm_email)

Should probably be...

if(email.equals(crfm_email))
Andrew White
  • 52,720
  • 19
  • 113
  • 137
2

Try :

email.equals(crfm_email)

== in java compares if it is the same instance of the string not if the content of the string are the same

Yahel
  • 8,522
  • 2
  • 24
  • 32
0

I think you might be interested in the following SO question: Java common Gotcha's.

Top contender is using == in stead of equals :)

Community
  • 1
  • 1
extraneon
  • 23,575
  • 2
  • 47
  • 51