title: How do I append to an already existing string in java?
body: <p>No, not like this:</p>
  <pre class="lang-java prettyprint-override"><code>String stringName = &quot;example&quot;;
  String stringName2 = (&quot;\&quot;&quot; + stringName + &quot;\&quot; secretly means \&quot;i like trains\&quot;&quot;);
  </code></pre>
  <p>The code I tried to use is:</p>
  <pre class="lang-java prettyprint-override"><code>long amountOfChars = chars.length;
  int amountOfTimesLeft = 20;
  String out = null;
  Random random = new Random();
  while (amountOfTimesLeft != 0) {
         long whichChar = (random.nextInt((int) (amountOfChars - 0 + 1)) + 0);
         StringBuilder sb = new StringBuilder(chars[(int) whichChar]);
         sb.append(out);
  }
  </code></pre>
  <p>(&quot;chars&quot; is an array of strings I've already defined)</p>
  <p>What I expected it to do is: Get a random string from the chars array and add it to the out string, and repeat it 19 more times.</p>
  <p>What it does:</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.ArrayIndexOutOfBoundsException: Index 32 out of bounds for length 32
      at Main.main(Main.java:45)
  </code></pre>
  <p>I have really no idea what this means. Could someone help?
  (line 45 is the line that defines sb)</p>
  <p>And yes, I know I forgot to decrease amountOfTimesLeft, but that's not the point of this question.</p>