android - Problems with Sqlite cursor.getString() in if statement -


i'm having problems if-statement , comparing strings. did check out know more string comparing. how compare strings in java?

if understood right "cursor.getstring(0)" return string. anyway everthing in sqlite table of type "text" guess. can print log.d string "name" , show correct word. when gets compare strings in if-statement not match anything. have no problem sqlite or other parts of code. if-statement not work

here 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                 }                 else if (food.equalsignorecase(name)){                    // ...do else                  }              } 

it continue print rest of column want "do something" if find name "food". can find out wrong if-statement?

just short thing, please don't name variable capital @ beginning (looks class). however, perhaps 1 of strings contains space or that. use method trim before compare 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     } else if (food.trim().equalsignorecase(name.trim())) {         // ...do else      } } 

if doesn't solve problem, need post more code, if solves problem, please rate answer.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -