1

Possible Duplicate:
How do I compare strings in Java?

I have this code its working fine in retreiving the value from the url, but its not recognizing that the string is "True" is the toString() what I need or something else?

try {
    URL url = new URL("http://www.koolflashgames.com/test.php?id=1");
    URLConnection yc = url.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(yc
            .getInputStream()));
    inputLine = in.readLine();
    inputLine = inputLine.toString();
    if(inputLine == "True") {
        logger.info(inputLine);
        player.sendMessage("Thanks");
    }else{
        logger.info(inputLine);
        player.sendMessage("HAHAHA");
    }
    in.close();
} catch (Exception e) {
    e.printStackTrace();
}
Community
  • 1
  • 1
arennaker
  • 53
  • 1
  • 11

4 Answers4

4

You cannot use == to compare the content of Strings, as they are objects. You have to create a method to compare objects. In the case of strings, you can use stringName.equals(otherString).

Clark
  • 1,357
  • 1
  • 7
  • 18
3

You must use equals to compare strings. Replace:

if(inputLine == "True") {

with:

if(inputLine.equals("True")) {

The operator == tells you if two references refer to the same object, not if the values are the same.

thedayofcondor
  • 3,860
  • 1
  • 19
  • 28
3

I beg to differ. Use .equalsIgnoreCase() method to compare the string ignoring the case. This will match all cases, such as "True", "TRue", "tRue".. etc approximately 16 matches.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
2

In order to compare String objects, use the equals() method.

The == operator checks whether the two Strings have the same reference.

See How do I compare strings in Java? for more info.

Community
  • 1
  • 1
eboix
  • 5,113
  • 1
  • 27
  • 38