Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6861643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:33:39+00:00 2026-05-27T02:33:39+00:00

I have this error while trying to run my program. I am still wondering

  • 0

I have this error while trying to run my program. I am still wondering why it wouldn’t not read the
other size of the square in magicData.txt, and I am getting an ArrayIndexOutOfBoundsException error, is this error due to the boolean magic class not returning whether it is true or not.

run:

******** Square 1 ********
8 1 6 3 5 7 4 9 2 the sum of row 0  is...: 15
the sum of row 1  is...: 15
the sum of row 2  is...: 15
the sum of columns 0 is....: 15
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
The sum of the main diagonal is......: 15
The sum of the other diagonal is.....: 15
Is it a magic square: true
the sum of columns 1 is....: 15
The sum of the main diagonal is......: 15
The sum of the other diagonal is.....: 15
Is it a magic square: true
the sum of columns 2 is....: 15
The sum of the main diagonal is......: 15
The sum of the other diagonal is.....: 15
Is it a magic square: true
    at assignment6.Square.sumCol(Square.java:42)
    at assignment6.SquareTest.main(SquareTest.java:36)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

SquareTest.java

import java.util.Scanner;
import java.io.*;
public class SquareTest {

public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(new File("magicData.txt"));
int count = 1; //count which square we're on
int size = scan.nextInt(); //size of next square
//Expecting -1 at bottom of input file
while (size != -1)
{
//create a new Square of the given size
    Square Size = new Square(size);
//call its read method to read the values of the square
    Size.readSquare(scan);

System.out.println("\n******** Square " + count + " ********");
//print the square
Size.printSquare();
//print the sums of its rows
for (int row = 0; row < size; row++) {
       System.out.println("the sum of row " + row + "  is...: " + Size.sumRow(row));}
//print the sums of its columns
for (int col = 0; col < size; col++) {
 System.out.println("the sum of columns " + col + " is....: "+ Size.sumCol(col));
//print the sum of the main diagonal
 System.out.println("The sum of the main diagonal is......: "+ Size.sumMainDiag());
//print the sum of the other diagonal
  System.out.println("The sum of the other diagonal is.....: "+ Size.sumOtherDiag());
//determine and print whether it is a magic square
 System.out.println("Is it a magic square: " + Size.magic());
//get size of next square
size = scan.nextInt();
count++;
}

}

}
}

Square.java

import java.util.Scanner;
public class Square {
int[][] square;

//--------------------------------------
//create new square of given size
//--------------------------------------
public Square(int size)
{
 square = new int [size][size];

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


}
//--------------------------------------
//return the sum of the values in the given column
//--------------------------------------
public int sumCol(int col)
{int sum = 0;
for (int i = 0; i < square.length; i++){
    sum = sum + square[i][col]; }
return sum;
}
 //--------------------------------------
 //return the sum of the values in the main diagonal
  //--------------------------------------
 public int sumMainDiag()
{int sum = 0;
 for (int i = 0; i < square.length; i++) {
  sum = sum + square[i][i];
}
   return sum;
 }
//--------------------------------------
//return the sum of the values in the other ("reverse") diagonal
//--------------------------------------
public int sumOtherDiag()
{int sum = 0;
 for (int i = 0; i < square.length; i++) {
 sum = sum + square[i][square.length - 1 - i];
 }
 return sum;

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

       for (int col = 0; col < square.length; col++) {
            if (sum != sumCol(col)) {
             magic = false;}



}
       }
}
return magic;
}
//--------------------------------------
//read info into the square from the input stream associated with the
//Scanner parameter
//--------------------------------------
public void readSquare(Scanner scan)
{
for (int row = 0; row < square.length; row++)
for (int col = 0; col < square.length; col ++)
square[row][col] = scan.nextInt();
}
//--------------------------------------
//print the contents of the square, neatly formatted
//--------------------------------------
public void printSquare()
{
     for (int row = 0; row < square.length; row++)
        for (int col = 0; col < square.length; col++) {
         System.out.print(square[row][col] + " ");

}
}
}

magicData.txt

3
8 1 6
3 5 7
4 9 2
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
4
48 9 6 39
27 18 21 36
15 30 33 24
12 45 42 3
3
6 2 7
1 5 3
2 9 4
4
3 16 2 13
6 9 7 12
10 5 11 8
15 4 14 1
5
17 24 15 8 1
23 5 16 14 7
4 6 22 13 20
10 12 3 21 19
11 18 9 2 25
7
30 39 48 1 10 28 19
38 47 7 9 18 29 27
46 6 8 17 26 37 35
5 14 16 25 34 45 36
13 15 24 33 42 4 44
21 23 32 41 43 12 3
22 31 40 49 2 20 11
-1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:33:39+00:00Added an answer on May 27, 2026 at 2:33 am

    The problem is that you are changing the value of size inside the loop where you iterate over all the columns:

    for (int col = 0; col < size; col++) {
       // etc...
    
       size = scan.nextInt();
    }
    

    You probably meant this:

    for (int col = 0; col < size; col++) {
       System.out.println("the sum of columns " + col + " is....: "+ Size.sumCol(col));
    }
    
    //etc...
    
    size = scan.nextInt();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error, while I'm testing the code below: You have an error
I am getting this error while running a hadoop pipes program. The program compiles
I am trying to organize my program into functions and have ran into this,
While I am trying to compile and run a program Friend Finder, which I
I have been receiving errors when trying to save and run this Python 3.1
I am facing this problem while trying to run my java file by writing
I am receiving a Segmentation Fault error when I run this program. To summarize,
While trying to run a string through PHP's htmlentities function, I have some cases
I have this error message: Msg 8134, Level 16, State 1, Line 1 Divide
I have this error that is keeping me from moving forward. I basically have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.