0

I have a method (par.getstatus) which outputs a message that looks exactly like this: 3::0::P:3::C:2::S:1::M:1::D:0::A:0::Q:0null

problem is when I write:

    String s = par.getStatus(3);
    assertTrue(s == "3::0::P:3::C:2::S:1::M:1::D:0::A:0::Q:0null");

it does not return true. I know that the string is the right one, cause I printed it to the console, and copied and pasted it directly, checked that I did that right many times. And to check that it is not the assertTrue method that I am using that is wrong, I wrote:

    String d = "rr";
            assertTrue(s == "rr");

which returns true.

Someone said that it could be null that is causing the problems, so i removed that, but still no luck. Does anyone know what the problem could be here? thanks

  • 1
    Please search for answers on SO... You should never compare strings (or, indeed, most objects) in java using the == comparison operator. Use `aString.compareTo(otherString)` or `aString.equals(otherString)`. – jgitter May 12 '14 at 12:44
  • thanks I did search but missed the other thread, will look harder next time. thanks for your help. – user1319364 May 12 '14 at 12:51

1 Answers1

0
s == "3::0::P:3::C:2::S:1::M:1::D:0::A:0::Q:0null"

== compares the references of two Strings and these two strings are not the same Object.

use String.equals() to compare the content of two Strings: Have look at this question.

assertTrue(s.equals("3::0::P:3::C:2::S:1::M:1::D:0::A:0::Q:0null"));
Community
  • 1
  • 1
Simulant
  • 19,190
  • 8
  • 63
  • 98