-1

I try to compare data from db to String but its not valid... I put to db few informations, one int for time and few Strings. Then i get information from db and store them as table and show on ListView. Now I try to compare the values to show specyfic data. I know values in my table/db so I try:

if (value[i][2] == "Plain"){
}

But it's don't work. So i Try:

if (value[i][2] != "Plain"){
}

And i get full list also with items where value[i][2] == "Plain". I also make toast when OnItemClickListener and showvalue[i][2]on it so I'm 100% surevalue[i][2]` it's Plain

Then I try:

String PlainTest = value[i][2];
if (value[i][2] == PlainTest){
}

And again it's correct... I also try value[i][2].trim() but still nothing Any idea what else I can check? I know value I get from db. I can show it on list, I can show it on toast. But somehow i can't compare it to string I don't get any errors. It's just like values don't match

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
Spun
  • 49
  • 4

3 Answers3

1

try:

value[i][2].equals("Plain")
eski
  • 7,917
  • 1
  • 23
  • 34
1

To compare strings use the equals method

value[i][2].equals("Plain")

cowls
  • 24,013
  • 8
  • 48
  • 78
  • Thanks... 2hr of searching and it's so simple :/ – Spun Mar 14 '13 at 13:39
  • :( Search on SO first there are lots of useful answers around – cowls Mar 14 '13 at 13:41
  • Yea i search here but i don't search compare strings. I search only compare values from db to string and don't get solid answer. My fault i should search deeper :) – Spun Mar 14 '13 at 13:43
  • Never mind, lesson learnt for next time! Make sure you read the links in the question comments – cowls Mar 14 '13 at 13:46
  • @Spun to explain this, since strings are objects, they are only == if they not only contain the same characters but ALSO refer to the same object in memory. if they reference different memory locations, then they are not ==. this messes up a LOT of beginners because when you write little test programs, Java will often reuse Strings from memory, meaning == comes out true. .equals compares the contents only, and so it is more reliable. – Jeff Hawthorne Mar 14 '13 at 13:53
  • @Jeff this is all covered in the links in question comments – cowls Mar 14 '13 at 13:54
  • @cowls most folks don't bother with links once they get their answer ;) – Jeff Hawthorne Mar 14 '13 at 13:57
  • 1
    I do and read it already :) – Spun Mar 14 '13 at 14:05
0

use equals to compare string

String PlainTest = value[i][2];
if (value[i][2].equals(PlainTest)) {
}
silly
  • 7,789
  • 2
  • 24
  • 37