I dont know why the following codition is always reuturning false even when its TRUE. What im comparing is result from a user entered text in a string variable 'classNameGlobal' and a second result from a DB query resulting a string value from a column of table. Whenever i run the control switches to else condition not running the if part -.-
Please have a look.
String result[];
result = new String[cursor.getCount() - 1];
int i = 0;
while (cursor.moveToNext()) {
result[i] = cursor.getString(cursor.getColumnIndex("name"));
i++;
}
classNameGlobal = className.getText().toString().toUpperCase().trim();
Note: All if the above code is written in onclick method except for that result array declaration. Also, Im using the same method for string being stored in database i.e. im converting it to upper case and then triminng any extra white spaces !!! Even when im dumping the results from query and tat of user entered string. Both ARE SAME. NOW why do im always keeping getting this else part running but not the if part -.- The method im using to compare the results from database query and user entered string is:
for (int b = 0; b < result.length; b++) {
if (classNameGlobal == result[b]) {
Log.d(TAG, "Inside if condition now : " + result[b] + " \t" + classNameGlobal);
db.execSQL("drop table if exists " + classNameGlobal);
/* Deleting Code File */
this.deleteFile(classNameGlobal + ".txt");
Toast.makeText(
this,
classNameGlobal + " Database Dropped Successfully ",
Toast.LENGTH_S开发者_如何学CHORT).show();
break;
} else {
Toast.makeText(
this,
"No Such Database Exists",
Toast.LENGTH_SHORT).show();
}
}
In Java, compare String contents with the equals method. Comparing with the == operator just checks whether two references are pointing to the same object.
So, change
classNameGlobal == result[b]
to
classNameGlobal.equals(result[b])
Use if (classNameGlobal.equals(result[b])) {
instead of ==
.
精彩评论