本文介绍了如果(...) - 两个等边被认为是不相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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
解决方案

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

这篇关于如果(...) - 两个等边被认为是不相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 18:47