Reputation: 2906
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
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
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
Reputation: 122381
Use:
if (email.equals(crfm_email))
{
....
to compare the strings. The point being you want to compare the contents of the String
s, not the references to the String
objects.
Upvotes: 3
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