-4

Possible Duplicate:
How do I compare strings in Java?

im new to android and this is my first post in StackOverflow.In short I face a very strange problem : in some methods I used if(...) ,and both of the values were equal ,yet it doesn't go through the if . Here is an example :

String []s=db.getStudentsNames();
        String []t=CopyNames(s);
        String t1,t2;

        t2=Id.getText().toString();
        for(int i=0;i<s.length;i++)
        {
            t1=t[i].substring(t[i].indexOf("-")+1).toString();
            Notifications(t[i].substring(t[i].indexOf("-")+1).toString());
            if(t1.toString()==t2.toString())//Problem!
            {
                Notifications("Id already exists for "+t[i].substring(0,t[i].indexOf("-")).toString());
                return false;
            }
        } 

The variables t1 & t2 are : t1="123456789" & t2="123456789" , yet it doesn't enter the if like they are not equal. And there are other places were two equal sides are considered as not equal - in the same java page (activity) like: if(add.getText().toString()=="Add Student") : add refers to a button which has ,by default , a text :"Add Student" so how can I solve this problem ?

**when this problem started , I started to see in LogCat:

 W/KeyCharacterMap(282): No keyboard for id 0

 W/KeyCharacterMap(282): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

 D/dalvikvm(282): GC_FOR_MALLOC freed 5438 objects / 256800 bytes in 73ms
Community
  • 1
  • 1
user1967122
  • 63
  • 1
  • 9
  • 4
    Use `t1.toString().equals(t2.toString())`. What you're doing right now is testing reference pointers in memory. – DeeV Jan 10 '13 at 15:38
  • 1
    Solution here: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Tomik Jan 10 '13 at 15:39

2 Answers2

4

You shouldn't use == to compare Strings in Java. Use t1.toString().equals(t2.toString()) instead.

Gereon
  • 17,258
  • 4
  • 42
  • 73
1

never use == on String objects. Use t1.equals(t2) instead.

mihail
  • 2,173
  • 19
  • 31