Code to build all possible strings, and find a specific one doesen't work as expected : <p>Part of a task I'm trying to solve is building all possible combinations of letters and whitespace. I'm trying to find a specific String, while building the combinations. I think i implemented the solution well, but my test tells me otherwise.</p>

<p>I'm taking the chars from an array and in a for loop I build a String from them, then print the solution. When I built all the combinations of let's say "1 character combinations", then i move on to "2 character combinations". I have a boolean to check if the contents of the StringBuilder is equal to the given String that i want to find. I checked, and the example String was indeed built, but the boolean didn't change.</p>

<pre><code>public static void main(String[] args) throws InterruptedException {

    findString("rxc");

}

public static void findString(String string) {
    boolean isFound = false;
    String allChars = "abcdefghijklmnopqrstuvwxyz ";
    char[] sequence = allChars.toCharArray();
    int lengthOfExpression = 3;
    //builder contains 3 whitespaces at the beginning
    StringBuilder builder = new StringBuilder("   ");

    int[] pos = new int[lengthOfExpression];
    int total = (int) Math.pow(allChars.length(), lengthOfExpression);
    for (int i = 0; i &lt; total; i++) {
        for (int x = 0; x &lt; lengthOfExpression; x++) {
            if (pos[x] == sequence.length) {
                pos[x] = 0;
                if (x + 1 &lt; lengthOfExpression) {
                    pos[x + 1]++;
                }
            }
            builder.setCharAt(x, sequence[pos[x]]);
        }
        pos[0]++;

        if (builder.toString() == string) {
            isFound = true;
        }

        System.out.println(builder.toString());
    }
    System.out.println(isFound);
}
</code></pre>

<p>Expected result would be a 'true' printed at the end. Instead, my result is as follows:</p>

<p>//lots of lines of combinations omitted</p>

<p>r<br>
s<br>
t<br>
u<br>
v<br>
w<br>
x<br>
y<br>
z  </p>

<p>false</p>
