title: How to fix out of bounds index error?
body: <p>FIXED:</p>
  
  <p>I removed my while loop and added</p>
  
  <pre><code> if (num.contains(count) == true) {
                  freq = Collections.frequency(num, count);
  
                  for (int k = 0; k &lt; freq; k++) {
                      sb.append(temp);
                  }
              }
  </code></pre>
  
  <p>I'm trying to add a random (well between 0-8) additional copies of letters to a word.  Example... does could turn into dddooees or doeess. </p>
  
  <p>My function works sometimes, but always crashes with an Out of Bounds index error.  </p>
  
  <p>I'm assuming I need to check for a NULL value my ArrayList at some point.  I tried wrapping my while statement with and if to check for it, but no improvement was made.  Any suggestions?</p>
  
  <pre><code>private static String addLetters(String word) {
          StringBuilder sb = new StringBuilder(word.length());
          String[] apart = word.split("");
          int rand = (int)(Math.random() * 8);
          int ran, goUp;
          int count = 0;
  
         List&lt;Integer&gt; num = new ArrayList&lt;&gt;();
  
          for (int i = 0; i &lt; rand; i++) {
              ran = (int)(Math.random() * word.length());
              num.add(ran);
          }
  
          Collections.sort(num);
  
          for (int temp : num) {
              System.out.println(temp);
          }
  
          for (String temp: apart) {
              goUp = count;
  
              sb.append(temp);
              System.out.printf("String so far: %s\n", sb.toString());
              System.out.printf("OUTSIDE: count: %d, goUp: %d\n", count, goUp);
  
        /*
          Removed the while loop and added in the above code using collections, works as intended now.
        */
              while (count == num.get(goUp)) {
                  System.out.printf("INSIDE: count: %d, goUp: %d\n", count, num.get(goUp));
                  sb.append(temp);
                  System.out.printf("String ADD extra: %s\n", sb.toString());
                  goUp++;
              }
              count++;
          }
          return sb.toString();
      }
  </code></pre>