Question

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();
}
Était-ce utile?

La solution

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).

Autres conseils

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.

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.

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top