4

resultString is the parameter I get from sqlite

resultString = result.getString(result.getColumnIndex(1));

I want to compare ignore case with user input , following is the code I have use. But it doesn't work.

For example, in database, I store a username "George" When I login, I have to type exactly "George"."george" doesn't work.

Here is my code to compare.

if (userName.equalsIgnoreCase(resultString)) {
    return true;
}

What might be the problem?

Squonk
  • 48,735
  • 19
  • 103
  • 135
Hieu Do
  • 51
  • 1
  • 1
  • 5
  • 2
    This looks like it should work. Have you tried hardcoding the test cases. So above your comparison type userName = "George" and resultString = "george"? Also can you print out userName and resultString before the comparison in your other cases to make sure there's not extra characters like whitespace? – nmjohn Jan 08 '12 at 06:58
  • I tried to hardcoding as u suggest and the "if" clause works.Still have no idea what happenning. – Hieu Do Jan 08 '12 at 07:02
  • if you're sure you're doing everything right, this is a hail mary, but could it be an encoding issue? – Yevgeny Simkin Jan 08 '12 at 07:04
  • I would now go through it normally and have it Log.d("TAG","X" + userName + "X Y" + resultString + "Y"); and see if you have your encoding right. – nmjohn Jan 08 '12 at 07:07
  • @nmjohn : when I try to print it out to log, I figure out that my query has problem. my where clause is username=?. So when I type in "george", the result string is empty.Thank you , I was panic and didn't try the log. – Hieu Do Jan 08 '12 at 07:31

4 Answers4

8

Please try following code,

if (userName.trim().equalsIgnoreCase(resultString.trim())) 
{     
       return true; 
} 
Lucifer
  • 29,392
  • 25
  • 90
  • 143
2

Your code should work if the only difference is the case. I suspect you have leading or trailing spaces. Try the following:

if (userName.trim().equalsIgnoreCase(resultString.trim())) {
    return true;
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

I was panic and didn't try the to print out the result.The problem was my query statement.

   String whereClause = DatabaseHelper.USER_COL + " name =?";

The resultString is always empty unless input is the same as data.

To fix it I follow instruction in this post sqlite LIKE problem in android

   String whereClause = DatabaseHelper.USER_COL + " LIKE ?";

   userName = userName+"%"
Community
  • 1
  • 1
Hieu Do
  • 51
  • 1
  • 1
  • 5
0
    int Num;
    String answer = et_q7.getText().toString();
    String right_answer = "Air Pollution";
    String right_answer2 = "Air";
    if (answer.trim().equalsIgnoreCase(right_answer.trim()) || answer.trim().equalsIgnoreCase(right_answer2.trim())) {
        Num = 1;
    } else {
        Num = 0;
    }