Matthew Wu
Matthew Wu

Reputation: 11

Why does it keep saying "ArrayIndexOutOfBoundsException" on my code?

so I'm new to coding and I am using Drjava.
I just got an assignment from my class to ask the user to input integers into two arrays (the user must input the integers from smallest to largest in each array).
Then I have to combine both arrays and order the new array from smallest to largest.
I think I figured out how to do this and it'll compile just fine, but it kept saying that there was an OutOfBoundsException after I ran the code and entered some numbers.

Why does it keep doing that and how do I fix it?

Thanks.

class Main{

     public static void main (String str[]) throws IOException {
          Scanner scan = new Scanner (System.in);

          System.out.println("Enter the values for the first array, up to 10000 values, enter a negative number to quit");

          int[] array1= new int[10000];
          int x=0;
          int x1=0;
          int v=1;
          for(int where=1; x>=0; where++) {
            x= scan.nextInt();
            array1[where]=x; 
            x1++;
            if((array1[where]<array1[where-1])&&(x>=0)){
              System.out.println("ERROR: Array not in correct order");
              x=-326;}
          }

          System.out.println("Enter the values for the second array, up to 10000 values, enter a negative number to quit");

          int[] array2= new int[10000];
          int y=0;
          int y1=0;
          int w=1;
          for(int wher=1; y>=0; wher++) {
            y= scan.nextInt();
            array2[wher]=y; 
            y1++;
            if((array2[wher]<array2[wher-1])&&(y>=0)){
              System.out.println("ERROR: Array not in correct order");
              y=-326;}
          }

          if(x!=-326) {
          System.out.println("First Array: ");
          int where=x1;
          for(v=1; v<(where); v++) {
            System.out.println(array1[v]); }}

          if(y!=-326) {
          System.out.println("Second Array: ");
          int wher=y1;
          for(w=1; w<(wher); w++) {
            System.out.println(array2[w]); }} 

          int[] array3= new int[v+w];
          int a=0;
          int b=0;
          while((a+b)<(v+w-3)) {
            while(array1[v-a]>array2[w-b]) {
              array3[w+v-a-b]=array1[v-a];
              a++;}
            while(array2[w-b]>=array1[v-a]) {
              array3[v+w-a-b]=array2[w-b];
               b++;}
          }

          if((y!=-326) && (x!=-326)) {
          System.out.println("Merged Array: ");
          int c=0;
          while((v+w-c)>=2){
            System.out.println(array3[v+w-c]);
            c++;                  }
          } 

     }

}

Upvotes: 0

Views: 179

Answers (1)

Chris Leyva
Chris Leyva

Reputation: 3526

In your first for loop, when where = 10000, it tries to access array1[10000], which doesn't exist. So you are trying to access an index that is "Out of Bounds".

You need to make sure that you don't try to access an index that doesn't exist.

Since you define array1 = new int[10000], that means that you can only access index 0 - 9999. Anything past array1[9999] will throw that same error "ArrayIndexOutOfBounds". It's a very clear error message, I think.

Upvotes: 2

Related Questions