Class Lab9

```
        ScalesSolution s = new ScalesSolution("00100");
        s.println();
        s.SmallChange();
        s.println();

```

Method SmallChange in the ScalesSolution class

```
public void SmallChange() {

    int n = scasol.length();
    System.out.println("The length of scasol is "+ n);
    //CS2004 method generates a random integer number between 0 and n
    int p = CS2004.UI(0,n);

    StringBuilder sb = new StringBuilder(scasol);
    if (scasol.charAt(p) == '0') {
        sb.setCharAt(p, '1');
    } else {
        sb.setCharAt(p, '0');
    }
    scasol = sb.toString();
}

```

After running the code multiple times, I sometimes get the error

```
"String index out of range 5"

```

Even though this prints out

```
  The length of scasol is 5

```

every time it is run

Error:
 00100
 The length of scasol is 5
 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
 at java.lang.String.charAt(String.java:658)
 at ScalesSolution.SmallChange(ScalesSolution.java:15) //point to if (scasol.charAt(p) == '0') 
 at Lab9.main(Lab9.java:9)

I don't understand how sometimes it goes out of bounds. Any help please?