woollyMammoth
woollyMammoth

Reputation: 105

Why is this generating an Index Out of Bounds error?

I have tried to figure out why the loop in createArithmeticSeq is going out of bounds, but can't figure it out. The loop should be stopping when i=listSize-1, since it's (i=0;i < listSize; i++). Can anyone shed some light on this for me?

import java.util.*;

public class MagicSquare 
{
static int row, col, n, rows, columns, listSize;
static Scanner console = new Scanner (System.in);

This is where the issue is occurring:

public static void createArithmeticSeq(int [] list)
    {   
    //prompt user for array size
    System.out.println("Enter size of array (in form nxn), n:");
    n = console.nextInt();
    rows = n;
    columns = n;
    listSize= (n*n);
    int first; 
    int diff;
    //prompt user for first and diff
    System.out.println("Enter first and diff : ");
    first = console.nextInt();
    diff  = console.nextInt();
    //process to create list of n*n elements 
    for (int i=0; i<listSize; i++)
    {
        list[i]=first+i*diff;
    }
}

And here is the rest of the code:

public static void matricize (int [] list, int [][] matrix)
{
    int i = 0;
//loop through each row
    for (row=0; row<matrix.length; row++)
    {
    //loop through each column
        for (col=0; col<matrix[row].length; col++)
        {
        //populate matrix with values from list
        matrix[row][col] = list[i++];
        }
     }
}
public static void printMatrix(int [][] matrix)
{

    for (row=0; row < matrix.length; row++)
    {
        for (col=0; col < matrix[row].length; col++)
            System.out.printf("%2d" + " ", matrix[row][col]);

        System.out.println("\n");
    }
}

public static void reverseDiagonal(int [] [] matrix)
{ 
    int temp;
    for (row=0; row<matrix.length / 2; row++)
    {
        temp = matrix[row][row];
        matrix[row][row] = 
            matrix[matrix.length - 1 - row] [matrix.length - 1 - row];
        matrix[matrix.length - 1 - row][matrix.length - 1 - row] = temp;
    }
    for (row=0; row<matrix.length / 2; row++)
    {
        temp = matrix[row][matrix.length - 1 - row];
        matrix[row][matrix.length - 1 - row] = 
            matrix[matrix.length - 1 - row][row];
        matrix[matrix.length - 1 - row][row] = temp;
    }
}

public static void magicCheck(int [] list, int [] [] matrix)
{
    int sum=0, sumRow=0, sumCol=0, sumDiag1=0, sumDiag2=0, magicNumber=0;

    for(int i=0; i<listSize; i++)
    {
        sum += list[i]; 
        magicNumber = sum /= 4;

    for(row=0; row<matrix.length; row++)
    {
            //sum each row, then compare to magicNumber
        for(col=0; col<matrix[row].length; col++)
        sumRow = sumRow + matrix[row][col];
        while (sumRow == magicNumber)
        {
            for(col=0; col<matrix.length; col++)
            {
                for(row=0; row<matrix[col].length; row++)
                {
                sumCol = sumCol + matrix[row][col];
                    while (sumCol == magicNumber)
                    {
                    sumDiag1 = matrix[0][0]+matrix[1][1]+matrix[2][2]+matrix[3][3];
                        while (sumDiag1 == magicNumber)
                        {
                        sumDiag2 = matrix[3][0]+matrix[2][1]+matrix[1][2]
                        +matrix[0][3];
                            while(sumDiag2 == magicNumber)
                            System.out.println("It is a magic square.");
                        }
                    }
                }
            }
        }
    }
    }
            System.out.println("It is not a magic square.");

}

public static void main (String [] args)
{
    int [] list = new int [listSize];
    createArithmeticSeq (list);
    int [] [] matrix = new int [rows] [columns];
    matricize(list, matrix);
    printMatrix(matrix);
    System.out.print("\n");
    reverseDiagonal(matrix);
    printMatrix(matrix);
    magicCheck(list, matrix);   
}

}

Upvotes: 0

Views: 178

Answers (3)

Pragmateek
Pragmateek

Reputation: 13374

In your main you create the list array using the uninitialized "listSize" value which will be 0. So you should change your code to first read the list size then creating the array:

public static void main (String [] args)
{
    //prompt user for array size
    System.out.println("Enter size of array (in form nxn), n:");
    n = console.nextInt();
    rows = n;
    columns = n;
    listSize= (n*n);

    int [] list = new int [listSize];
    createArithmeticSeq (list);
    int [] [] matrix = new int [rows] [columns];
    matricize(list, matrix);
    printMatrix(matrix);
    System.out.print("\n");
    reverseDiagonal(matrix);
    printMatrix(matrix);
    magicCheck(list, matrix);   
}

And your function becomes:

public static void createArithmeticSeq(int [] list)
{   
    int first; 
    int diff;
    //prompt user for first and diff
    System.out.println("Enter first and diff : ");
    first = console.nextInt();
    diff  = console.nextInt();
    //process to create list of n*n elements 
    for (int i=0; i<listSize; i++)
    {
        list[i]=first+i*diff;
    }
}

Upvotes: 2

Pradeep Pati
Pradeep Pati

Reputation: 5919

When you define listsize as static, it gets the default value of 0. And then you call createArithmeticSeq(int [] list), you are basically passing a list with space for 0 elements. If you change the value of listsize after you have created int [] list = new int [listSize];, the size of list doesn't change. Hence, you have to create the array after you know what size it would be.

To do that, modify your main method as

public static void main (String [] args)
{
    System.out.println("Enter size of array (in form nxn), n:");
    n = console.nextInt();
    rows = n;
    columns = n;
    listSize= (n*n);    
    int [] list = new int [listSize];
    createArithmeticSeq (list);
    int [] [] matrix = new int [rows] [columns];
    matricize(list, matrix);
    printMatrix(matrix);
    System.out.print("\n");
    reverseDiagonal(matrix);
    printMatrix(matrix);
    magicCheck(list, matrix);   
}

And your createArithmeticSeq method as

public static void createArithmeticSeq(int [] list)
{   
    int first; 
    int diff;
    //prompt user for first and diff
    System.out.println("Enter first and diff : ");
    first = console.nextInt();
    diff  = console.nextInt();
    //process to create list of n*n elements 
    for (int i=0; i<listSize; i++)
    {
        list[i]=first+i*diff;
    }
}

Upvotes: 3

Joan
Joan

Reputation: 426

When you are creating list array in main you are using listSize. Since you don't initialice it, its value is 0.

In createArithmeticSeq method you change it's size to n*n, but this doesn't efect the size of the array witch still is 0.

Upvotes: 4

Related Questions