rahul
rahul

Reputation: 2906

Error in if statement

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...!

Upvotes: 0

Views: 157

Answers (4)

extraneon
extraneon

Reputation: 23950

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

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

Upvotes: 0

Yahel
Yahel

Reputation: 8550

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

Upvotes: 2

trojanfoe
trojanfoe

Reputation: 122381

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.

Upvotes: 3

Andrew White
Andrew White

Reputation: 53496

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

Upvotes: 2

Related Questions