title: How to resolve "java.lang.IndexOutOfBoundsException" error?
body: <p>This is my code. It is a random string generator. When I run it, if the number of letters and characters added together are less than the desired amount of digits, I get an error code as shown below. Can someone help me on fixing this?</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  import java.util.Random;
  import java.util.Scanner;
  
  public class list {
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
          Random num = new Random();
          System.out.println("How many characters do you want?");
          Scanner Digits = new Scanner(System.in);
          int digits = Integer.parseInt(Digits.nextLine());
          System.out.println("How many different numbers do you want? 1-10 please");
          int number = Integer.parseInt(Digits.nextLine());
          System.out.println("Which letters do you want? One at a time, press enter after each letter. If done, just press enter.");
          List&lt;String&gt; characters = new ArrayList&lt;String&gt;();
          while (true) {
              System.out.println("Add your characters.");
              String character = Digits.nextLine();
              if (character.equals("")) {
                  break;
              } else {
                  characters.add(character);
              }
          }
  
          List&lt;Object&gt; andrew = new ArrayList&lt;Object&gt;();
          for (int x = 0; x &lt; digits; x++) {
              andrew.add(num.nextInt(number));
              andrew.add(characters.get(x));
          }
  
          for (int x = 0; x &lt; andrew.size(); x += 2) {
              System.out.print(andrew.get(x));
          }
      }
  }
  </code></pre>
  
  <p>Exception:</p>
  
  <pre><code>Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
  at java.util.ArrayList.rangeCheck(Unknown Source)
  at java.util.ArrayList.get(Unknown Source)
  at list.main(list.java:33)
  </code></pre>