Gabrielle
Gabrielle

Reputation: 4981

Compare two things doesn't work

I have a strange problem in my android app. I must compare two string which are equals. I tried this :

if (raspunsdata.equals(rok)) {
                System.out.println("changed ");

            } else
                System.out.println("no change");
        }

but I get always "no change". Before this I have System.out.println for both strings, and both of them have the same value.

I tried also (raspunsdata==rok) and raspunsdata.contentEquals(rok) but I have the same problem. Why? I cant understand this.,...please help...

Upvotes: 0

Views: 193

Answers (2)

blessanm86
blessanm86

Reputation: 31779

You might have unwanted white spaces. Might need to use the trim function just to make sure.

if (raspunsdata.trim.equals(rok.trim())) { 
    System.out.println("changed "); 
} else 
    System.out.println("no change"); 
} 

Btw equals is the correct way to check whether the values are the same.

Upvotes: 4

Alex Orlov
Alex Orlov

Reputation: 18107

.equals - compares the values of both objects. If you have 2 Strings with the same characters sets .equals will return true; == - compares if two objects references are equal. For example:

String a = "lol";
String b = a;

a == b - will be true.

Try reading: http://www.devdaily.com/java/edu/qanda/pjqa00001.shtml

Upvotes: 0

Related Questions