如果(...) - 两个等边被认为是不相等 [英] if(...) - two equal sides are considered as not equal
问题描述
可能重复:结果
如何比较Java中的字符串?
块引用>即时通讯新到Android,这是我第一次StackOverflow.In短后我面临一个很奇怪的问题:
在一些方法中我使用,如果(...),并且两个值均相等,但它不会通过如果去。
下面是一个例子:的String [] S = db.getStudentsNames();
的String [] T = CopyNames(S);
串T1,T2; T2 = Id.getText()的toString()。
的for(int i = 0; I< s.length;我++)
{
T1 = T [i]于.substring(T [i]于.indexOf( - )+1)的ToString();
通知(T [i]于.substring(T [i]于.indexOf( - )+1)的ToString());
如果(t1.toString()== t2.toString())//问题!
{
通知(标识已经存在于+ T [I] .substring(0,T [I] .indexOf( - ))的toString());
返回false;
}
}变量T1&安培; T2是:
T1 =123456789
&安培;T2 =123456789
,但它不喜欢,如果他们不相等进入。
还有其他地方有两个边相等被认为是不相等的 - 在同一个Java页面(活动),如:如果(add.getText()的toString()==添加学生。)
:添加是指有一个按钮,默认情况下,文本:添加学生
所以我怎么能解决这个问题?**时,这个问题开始,我就开始在LogCat中看到:
W / KeyCharacterMap(282):无键盘ID 0 W / KeyCharacterMap(282):使用默认的键盘对应:/system/usr/keychars/qwerty.kcm.bin D / dalvikvm(282):GC_FOR_MALLOC释放5438对象/在73ms 256800字节
解决方案您不应使用
==
在Java中比较字符串。使用t1.toString()。等于(t2.toString())
代替。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
解决方案You shouldn't use
==
to compare Strings in Java. Uset1.toString().equals(t2.toString())
instead.这篇关于如果(...) - 两个等边被认为是不相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!