android 代码中的字符串比较错误
String Comparison error in a android code
我正在使用 Sqlite 数据库保存和检索的简单代码。我的问题是我的代码总是需要 else 部分,即使字符串相同。
Toast.makeText(getApplicationContext(), "No UName: "+utext+","+str+"; Pswd: "+ptext+","+str1, Toast.LENGTH_LONG).show();
而不是,
Toast.makeText(getApplicationContext(), "Yes UName: "+utext+","+str+"; Pswd: "+ptext+","+str1, Toast.LENGTH_LONG).show();
我用 toast 和调试模式检查了输出。 utext 的值等于 str,ptext 的值等于 str1。我在这里犯了什么错误吗?我没有找到任何东西。
http://postimg.org/image/huvmxhgup/
public void onClick(View v) {
utext=t1.getText().toString();
ptext=t2.getText().toString();
Cursor c = db.rawQuery("SELECT * FROM " + tbl_name+" WHERE USERNAME= '"+utext+"' AND PASSWORD= '"+ptext+"'", null);
if(c.moveToLast() ) {
str=c.getString(c.getColumnIndex("USERNAME"));
str1=c.getString(c.getColumnIndex("PASSWORD"));
Toast.makeText(getApplicationContext(), "True : "+str+", "+str1, Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getApplicationContext(), "FALSE", Toast.LENGTH_LONG).show();
if(str==utext || str1==ptext)
Toast.makeText(getApplicationContext(), "Yes UName: "+utext+","+str+"; Pswd: "+ptext+","+str1, Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "No UName: "+utext+","+str+"; Pswd: "+ptext+","+str1, Toast.LENGTH_LONG).show();
}
在 java 中,==
比较对象引用而不是内容。
==
checks whether two strings are the same object,whereas
.equals()
or .compareTo()
checks whether the two strings have the
same value.
您可以使用 java 中的方法 .equals()
或 .compareTo()
来比较字符串
因此,替换
if(str==utext || str1==ptext)
和
if(str.equals(utext) || str1.equals(ptext))
或
if(str.compareTo(utext)==0 || str1.compareTo(ptext)==0)
您应该始终使用 .equals()
进行字符串比较。将您的代码更改为此。
if(str.equals(utext) || str1.equals(ptext))
Toast.makeText(getApplicationContext(), "Yes UName: "+utext+","+str+"; Pswd: "+ptext+","+str1, Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "No UName: "+utext+","+str+"; Pswd: "+ptext+","+str1, Toast.LENGTH_LONG).show();
我正在使用 Sqlite 数据库保存和检索的简单代码。我的问题是我的代码总是需要 else 部分,即使字符串相同。
Toast.makeText(getApplicationContext(), "No UName: "+utext+","+str+"; Pswd: "+ptext+","+str1, Toast.LENGTH_LONG).show();
而不是,
Toast.makeText(getApplicationContext(), "Yes UName: "+utext+","+str+"; Pswd: "+ptext+","+str1, Toast.LENGTH_LONG).show();
我用 toast 和调试模式检查了输出。 utext 的值等于 str,ptext 的值等于 str1。我在这里犯了什么错误吗?我没有找到任何东西。
http://postimg.org/image/huvmxhgup/
public void onClick(View v) {
utext=t1.getText().toString();
ptext=t2.getText().toString();
Cursor c = db.rawQuery("SELECT * FROM " + tbl_name+" WHERE USERNAME= '"+utext+"' AND PASSWORD= '"+ptext+"'", null);
if(c.moveToLast() ) {
str=c.getString(c.getColumnIndex("USERNAME"));
str1=c.getString(c.getColumnIndex("PASSWORD"));
Toast.makeText(getApplicationContext(), "True : "+str+", "+str1, Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getApplicationContext(), "FALSE", Toast.LENGTH_LONG).show();
if(str==utext || str1==ptext)
Toast.makeText(getApplicationContext(), "Yes UName: "+utext+","+str+"; Pswd: "+ptext+","+str1, Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "No UName: "+utext+","+str+"; Pswd: "+ptext+","+str1, Toast.LENGTH_LONG).show();
}
在 java 中,==
比较对象引用而不是内容。
==
checks whether two strings are the same object,whereas.equals()
or.compareTo()
checks whether the two strings have the same value.
您可以使用 java 中的方法 .equals()
或 .compareTo()
来比较字符串
因此,替换
if(str==utext || str1==ptext)
和
if(str.equals(utext) || str1.equals(ptext))
或
if(str.compareTo(utext)==0 || str1.compareTo(ptext)==0)
您应该始终使用 .equals()
进行字符串比较。将您的代码更改为此。
if(str.equals(utext) || str1.equals(ptext))
Toast.makeText(getApplicationContext(), "Yes UName: "+utext+","+str+"; Pswd: "+ptext+","+str1, Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "No UName: "+utext+","+str+"; Pswd: "+ptext+","+str1, Toast.LENGTH_LONG).show();