samedi 5 septembre 2015

Java programming substrings and why if-statement is doing the wrong thing? [duplicate]

This question already has an answer here:

I'm quite new to java so I'm quite stuck in whats wrong with this code:

    String word = "ooxxoo"; 
    int start = 0; 

    String result = word.substring(start+2, (start+4));
    String answer = "xx"; 

    if (result == "xx") {
        System.out.println("True"); 
    } else {
        System.out.println("False"); 
    }

The other parts of the code aren't important its just the main class to run the program and some other things I'm testing out.

The problem here is that the result prints "False" when its supposed to be "True". I made result = word.substring(start+2, (start+4)), and that is substring the word "ooxxoo" into "xx" exactly.

I tried printing out this "result" variable and it does indeed become "xx" but for some reason when put in the IF statement of whether "result" is equal to "xx" it returns false...

Aucun commentaire:

Enregistrer un commentaire