0

I'm having problems with an IF-statement and comparing strings. I did check this out to know more about string comparing. How do I compare strings in Java?

If I understood this right "cursor.getString(0)" will return a string. Anyway everthing in my Sqlite table is of the type "text" I guess. I can print with log.d the string "name" and that show the correct word. But when it gets to compare strings in the IF-statement it does not match anything. I have no problem with Sqlite or any other parts of the code. My if-statement does not work

Here is my code:

            String Food = "Food";                
            while(cursor.moveToNext()){
                String name = cursor.getString(0);
                Log.d("name", "name: " + name); //This sprints: name: Food
                if(name.equals(Food)){
                   // ...do something
                }
                else if (Food.equalsIgnoreCase(name)){
                   // ...do something else 
                }
             }

It will just continue to print the rest of the column but I want to "do something" if I find a name "Food". Can someone help find out what is wrong with my IF-statement?

Community
  • 1
  • 1
user2034859
  • 577
  • 1
  • 9
  • 20

1 Answers1

0

just a short thing, please don't name a variable with a capital at the beginning (looks like a class). However, perhaps one of the Strings contains a space or something like that. Use the method trim before you compare your strings:

String food = "Food";                
while(cursor.moveToNext()){
    String name = cursor.getString(0);
    Log.d("name", "name: '" + name + "'"); //This sprints: name: 'Food' ?
    if(name.trim().equals(food.trim())){
        // ...do something
    } else if (food.trim().equalsIgnoreCase(name.trim())) {
        // ...do something else 
    }
}

If this doesn't solve your problem, you need probably post more code, if it solves your problem, please rate this answer.

speedy1034
  • 304
  • 2
  • 9
  • Hi Thanks for your reply but that did not solve it...sorry:) Should not be that hard because it's just strings. – user2034859 Apr 14 '14 at 16:16
  • Is the string `food` hardcoded as in your example? Try to log this, too and look if you really have the string in food, which you are expecting. – speedy1034 Apr 14 '14 at 16:31
  • I did log both strings and they look alike. – user2034859 Apr 15 '14 at 05:40
  • Okay, perhaps a problem of encoding? `String newString = new String(oldString.getBytes("UTF-8"), "UTF-8");` Try to do this with both string before comparision. – speedy1034 Apr 15 '14 at 07:28
  • I have been staring at this string now for a long time. I thought that therre must be something wrong. I have ckecked the database file and that name in diffrent Sqlite browsers but they all look the same. – user2034859 Apr 15 '14 at 10:11
  • I have been staring at this string now for a long time. I thought that therre must be something wrong. I have ckecked the database file and that name in diffrent Sqlite browsers but they all look the same. But here comes the bitter part. If I took a closer look, I mean a really closer look there is an almost not noticeable diffrence, like a "'" combined with the first "'F". It's more like a combined T and F...Trim did not help but if I used endsWith(food) then it worked. I will leave this for now and continue the coding and investigate this futrher later:) Thanks for all your help! – user2034859 Apr 15 '14 at 10:19