title: How do I overcome java.lang.ArrayIndexOutOfBoundsException: 2 in this code?
body: <p>When I tested this code, it gave me java.lang.ArrayIndexOutOfBoundsException: 2
  The code isn't finished yet but this is what I am trying to do:
  A program that asks the user for two numbers. Print all numbers from the smaller
  number to the larger number (inclusive) in a random order. Using this order, find the
  largest sum of 2 numbers adjacent to one another.
  Could some please point out to me what needs to be fixed as well as how to fix it? Thanks.</p>
  
  <pre><code> boolean isNum = false;
      int in1 = 0;
      int in2 = 0;
      int size = 0;
      int largestsum = 0;
      while (!isNum) {
          try {
              System.out.print("Enter a number: ");
              in1 = Integer.parseInt(in.readLine());
              boolean isSame = false;
              while (!isSame) {
                  System.out.print("Enter a number: ");
                  try {
                      in2 = Integer.parseInt(in.readLine());
                      if (in1 != in2) {
                          isSame = true;
                      } else {
                          System.out.println("Same number.");
                      }
                  } catch (NumberFormatException nfe) {
                      System.out.println("Not a number.");
                  }
              }
              isNum = true;
          } catch (NumberFormatException nfe) { //catches error
              System.out.println("Not a number."); //informs user of the error
          }
      }
      if (in1 &gt; in2) {
          size = in1 - in2 + 1;
          int[] num = new int[size];
          for (int i = 0; i &lt; size; i++) {
              num[i] = in2 + i;
          }
          int[] order = new int[size];
          boolean[] used = new boolean[size];
          for (int i = 0; i &lt; size; i++) {
              int r = (int) ((size) * (Math.random()));
              while (used[r]) {
                  r = (int) ((size) * Math.random());
              }
              order[i] = r; //fill array
              used[r] = true; //r is now used
          }
          for (int i = 0; i &lt; size; i++) {
              if (num[order[i]] + num[order[i + 1]] &gt; largestsum) {
                  largestsum = num[order[i]] + num[order[i + 1]];
              }
          }
  </code></pre>