title: Array Index Out Of Bounds Exception: 2
body: <p>I get errors when after i enter the exits it says</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
  </code></pre>
  
  <p>Im terrible at programming so please bear with me </p>
  
  <pre><code>import java.util.*;
  
  public class Highway{
  
      public static void main(String[] args) {
  
          Scanner input = new Scanner(System.in);
  
          System.out.println("Total number of the exits");
          int p=input.nextInt();
          System.out.println("The first exit");
          int i=input.nextInt();
          System.out.println("The second exit");
          int j=input.nextInt();
  
          int[]A= new int[p];
  
          for(int w=0;w&lt;=p;i++) {
  
  
              A[i]=(int )(java.lang.Math.random() * 20 + 1);
              System.out.println(A[w]);
          }
      }
  
      static void Distance(int i, int j, int[] A) {
      // a is the array of distance
      // this find the  distances between i and j
          int distance = 0;
  
          if(j&gt;i){
          for(int k = i; k&lt;=j;k++) {
  
                  distance=distance+A[k];
          }
  
                  distance=distance-A[i];
  
          }
  
          if(i&gt;j){
              for (int m = j; m&lt;=i; m++){distance=distance+A[m];
              }
                  distance=distance-A[j];
              }
  
              System.out.println("The distance of the first"+i+" and second exit"+j+" is"+distance);
  
          }
  }
  </code></pre>