Home > Net >  Exception in thread main Java
Exception in thread main Java

Time:12-16

This is my program to print natural numbers from 1 to N^2 in a clockwise spiral. I’m getting the following error

     Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -2147483648 
out of bounds for length 3
    at Spiral.main(Spiral.java:13)

This is my program

class Spiral{
   public static void main(String[] args) {
       System.out.println("Enter value of N"); 
       Scanner sc=new Scanner(System.in);
       int N=sc.nextInt();
       int arr[][]=new int[N][N];
       int r1=0, c1=0, r2=N-1, c2=N-1, flag=1; int i=0,j=0;
       while(flag<=N*N)
      { for(j=c1;j<=c2;j  )
       arr[r1][j]=flag  ;
       
       
       for( i=r1 1;i<=r2;i  )
        arr[i][c2]=flag  ;  //this is the line of error
       
       
       for(j=c2-1;j>=c1;j--)
        arr[r2][j]=flag  ;
       
       
       for(i=r2-1; i>r1 1;i--)
        arr[i][c1]=flag  ;
       
       
       r1  ; r2--; c1  ; c2--; 
      }
       System.out.println("The Circular Matrix is:");
for( i=0;i<N;i  )
{
for( j=0;j<N;j  )
{
System.out.print(arr[i][j]  "\t");
}
System.out.println();
} 
}
}

The code works fine for N=2 but starts giving this error for N=3,4 etc. If N=3, the greatest value of arr[i][c2] will be arr[2][2] which falls in the range of a 3x3 matrix. Could someone explain why I’m getting this error then?

CodePudding user response:

Increment/decrement r1, c2, r2, and c1 appropriately after their corresponding for loops rather than at the end of the while loop:

import java.util.Scanner;

class Spiral {

    public static void main(String[] args) {
        System.out.println("Enter value of N");
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[][] arr = new int[N][N];
        int r1 = 0, c1 = 0, r2 = N - 1, c2 = N - 1, flag = 1;
        int i = 0, j = 0;
        while (flag <= N * N) {
            for (j = c1; j <= c2; j  )
                arr[r1][j] = flag  ;
            r1  ;
            for (i = r1; i <= r2; i  )
                arr[i][c2] = flag  ;
            c2--;
            for (j = c2; j >= c1; j--)
                arr[r2][j] = flag  ;
            r2--;
            for (i = r2; i >= r1; i--)
                arr[i][c1] = flag  ;
            c1  ;
        }
        System.out.println("The Circular Matrix is:");
        for (i = 0; i < N; i  ) {
            for (j = 0; j < N; j  ) {
                System.out.print(arr[i][j]   "\t");
            }
            System.out.println();
        }
    }

}

Example Usage 1:

Enter value of N
4
The Circular Matrix is:
1   2   3   4   
12  13  14  5   
11  16  15  6   
10  9   8   7   

Example Usage 2:

Enter value of N
7
The Circular Matrix is:
1   2   3   4   5   6   7   
24  25  26  27  28  29  8   
23  40  41  42  43  30  9   
22  39  48  49  44  31  10  
21  38  47  46  45  32  11  
20  37  36  35  34  33  12  
19  18  17  16  15  14  13  

CodePudding user response:

In the dugger you can see that you have an have in the second fpr loop an IndexOutOfBoundsException because i is 3. Due to the while loop wich is then always true because no condition of the for loops are true, r1 & c1will go up to the int max value and then overflow. You for loop are wrong

CodePudding user response:

You need to exit if your start row is greater than end row or start col is greater than end col based on your logic.

Add this in your while loop

if (r1 > r2 || c1 > c2) break;
  • Related