1

I'm writing a program that reads a file and sees if the information make up a magic square, but I am getting this error: "java.lang.ArrayIndexOutOfBoundsException: 3" in my sumCol method. Specifically the line below: for (int row = 0; row < square[row].length; row++) {

public int sumCol(int col) {
    int sum = 0;
    for (int row = 0; row < square[row].length; row++) {
        sum += square[row][col];
    }
    return sum;
}



I'm not sure if it is necessary, but here is the rest of my class as well:

// ****************************************************************

// Square.java
//
// Define a Square class with methods to create and read in
// info for a square matrix and to compute the sum of a row,
// a col, either diagonal, and whether it is magic.
//         
// ****************************************************************
import java.util.Scanner;

public class Square {

int[][] square;

//--------------------------------------
//create new square of given size
//--------------------------------------
public Square(int size) {

    square = new int[size][size];

    for (int row = 0; row < square.length; row++) {
        for (int col = 0; col < square.length; col++) {
            square[row][col] = row * 10 + col;
        }
    }

}

//--------------------------------------
//return the sum of the values in the given row
//--------------------------------------
public int sumRow(int row) {
    int sum = 0;
    for (int col = 0; col < square.length; col++) {
        sum += square[row][col];
    }
    return sum;
}

//--------------------------------------
//return the sum of the values in the given column
//--------------------------------------
public int sumCol(int col) {
    int sum = 0;
    for (int row = 0; row < square[row].length; row++) {
        sum += square[row][col];
    }
    return sum;
}

//--------------------------------------
//return the sum of the values in the main diagonal
//--------------------------------------
public int sumMainDiag() {
    int sum = 0;
    for (int j = 0; j < square.length; j++) {
        sum += square[j][j];    //you can do this because a square's diagonals have the same coordinate points
    }
    return sum;
}

//--------------------------------------
//return the sum of the values in the other ("reverse") diagonal
//--------------------------------------
public int sumOtherDiag() {
    int sum = 0;
    for (int j = 0; j < square.length; j++) {
        sum += square[j][square.length - 1 - j];
    }
    return sum;
}

//--------------------------------------
//return true if the square is magic (all rows, cols, and diags have
//same sum), false otherwise
//--------------------------------------
public boolean magic() {
    boolean answer = true;
    int sum = sumMainDiag();
    if (sumOtherDiag() != sum) {
        answer = false;
    } else {
        for (int col = 0; col < square.length; col++) {
            if (sum != sumCol(col)) {
                answer = false;
            }
        }
        for (int row = 0; row < square.length; row++) {
            if (sum != sumRow(row)) {
                answer = false;
            }
        }
    }
    return answer;
}

//--------------------------------------
//read info into the square from the input stream associated with the
//Scanner parameter
//--------------------------------------
public void readSquare(Scanner scan) {
    for (int[] square1 : square) {
        for (int col = 0; col < square.length; col++) {
            square1[col] = scan.nextInt();
        }
    }
}

//--------------------------------------
//print the contents of the square, neatly formatted
//--------------------------------------
public void printSquare() {
    for (int[] square1 : square) {
        for (int col = 0; col < square1.length; col++) {
            System.out.print(square1[col] + "\t");
        }
        System.out.println();
    }

}

}

4 Answers 4

1

change

row < square[row].length

to:

row < square.length
Sign up to request clarification or add additional context in comments.

Comments

1

Perhaps you meant this?

public int sumCol(int col) {
    int sum = 0;
    for (int row = 0; row < square.length; row++) {
        sum += square[row][col];
    }
    return sum;
}

Notice that the number of rows is given by square.length, whereas the number of columns in a given row is given by square[row].length.

2 Comments

That produced an infinite loop... Well not infinite, but quite close
@user2923395 no, it doesn't. The problem must be elsewhere, use a debugger to find the real culprit.
1

You're doing the check wrong. Use:

public int sumCol(int col) {
    int sum = 0;
    for (int row = 0; row < square.length; row++) {
        sum += square[row][col];
    }
    return sum;
}

You're checking the length of the row, not of the overall array.

2 Comments

That produced an infinite loop... Well not infinite, but quite close
@user2923395 Then the loop is elsewhere in the logic. I suggest singlestepping your code with a debugger.
1

I think you want

// square.length not square[row].length
for (int row = 0; row < square.length; row++) {
  // Here's where we might check square[row].length to be safe.
  if (col < square[row].length) {
    sum += square[row][col];
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.