Java - If statement doesn't work properly
<p>I'm new to this forum and after using it successfully to answer many questions in the past I finally came up with a question I can't figure out myself. Straight to the point: There is a binary String variable scacol and I need to change a random element of it to 0 if it was 1 before and the other way around. </p>

<p>I am performing the following tasks: </p>

<pre><code>1)Create a random integer p that ranges between 0 and n-1.
2)Create an empty new string say x.
3)Copy from elements 0 to p-1 from scasol to x.
4)Copy the changed version of position p of string scasol to x.
5)Copy from p+1 to n-1 of scasol to x.
6)Set scasol to be x.
</code></pre>

<p>This is what I've done so far: </p>

<pre><code>public void SmallChange() {
    String x = new String();
    int p = (int) (Math.random() * (scasol.length()));
    for (int i = 0; i &lt;= p - 1; i++) {
        x = x + scasol.charAt(i);
    }

    if (scasol.charAt(p) == 0) {
        x = x + 1;
    } else {
        x = x + 1;
    }
    for (int j = p + 1; j &lt;= (scasol.length() - 1); j++) {
        x = x + scasol.charAt(j);
    }
    scasol = x;
}
</code></pre>

<p>I can't understand why the if statement doesn't function properly and it always jumps to the else statement. So if the scasol = "00000" it does what it needs to. but if it is = "111111" there is no change. Thanks</p>
