8 Replies - 1074 Views - Last Post: 12 November 2014 - 11:49 PM Rate Topic: -----

#1 JavaNewbie82   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 21
  • Joined: 07-October 14

Can not figure out what is going on with my MagicSquare Program

Posted 12 November 2014 - 10:07 PM

I need help please. I am having trouble figuring out why my program is not running properly. When I run it I get
******** Square 1 ********
8	1	6	
3	5	7	
4	9	2	
The sum of the row 0 is: 15
The sum of the row 1 is: 15
The sum of the row 2 is: 15
The sum of the column 0 is: 15
The sum of the column 1 is: 15
The sum of the column 2 is: 15
The sum of the main diagonal is: 15
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
	at Square.sumOtherDiag(Square.java:69)
	at SquareTest.main(SquareTest.java:47)



// ****************************************************************
// SquareTest.java
//
// Uses the Square class to read in square data and tell if 
// each square is magic.
//          
// ****************************************************************
import java.io.*;
import java.util.Scanner;


public class SquareTest
{
    public static void main(String[] args) throws IOException
    {
    	Scanner fileScan = new Scanner(new File("magicData.txt"));

    	int count = 1;                 //count which square we're on
    	int box = fileScan.nextInt();     //size of next square

    	//Expecting -1 at bottom of input file
    	while (box != -1)
	    {
			//create a new Square of the given size
			Square magicSquare = new Square(box);
			
			//call its read method to read the values of the square
			magicSquare.readSquare(fileScan);
	
			System.out.println("\n******** Square " + count + " ********");
			
			//print the square
			magicSquare.printSquare();
				
			//print the sums of its rows
			for (int row = 0; row < box; row++)
				System.out.println("The sum of the row " + row + " is: " + magicSquare.sumRow(row));
		
			//print the sums of its columns
			for (int column = 0; column < box; column++)
				System.out.println("The sum of the column " + column + " is: " + magicSquare.sumColumn(column));
			
			//print the sum of the main diagonal
				System.out.println("The sum of the main diagonal is: " + magicSquare.sumMainDiag());
				
			//print the sum of the other diagonal
				System.out.println("The sum of the other diagonal is: " + magicSquare.sumOtherDiag());
				
			//determine and print whether it is a magic square
				System.out.println("The square is magic: " + magicSquare.isMagic());
				
			//get size of next square
			System.out.println("Next Square");
			box = fileScan.nextInt();
			count++;
	    }
    	
   }
}



// ****************************************************************
// 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;
import java.util.ArrayList;
public class Square
{
    int[][] table;
	int[][] square;
	int total, i, j;

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


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

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

    //--------------------------------------
    //return the sum of the values in the main diagonal
    //--------------------------------------
    public int sumMainDiag(){
    	total = 0;
    	for (i = 0; i < square.length; ++i)
    		total += square[i][j];
    	return total;
    }


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


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


    //--------------------------------------
    //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(i = 0; i < square.length; ++i){
    		for(int j = 0; j<square.length; ++j)
    			System.out.print(square[i][j] + "\t");
    		System.out.println();
    	}
    }

}




not sure why the second code didnt post properly. Where is the edit button???

Also, here is the text file

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

This post has been edited by macosxnerd101: 12 November 2014 - 10:34 PM
Reason for edit:: Added code tags around Square class and output


Is This A Good Question/Topic? 0
  • +

Replies To: Can not figure out what is going on with my MagicSquare Program

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Can not figure out what is going on with my MagicSquare Program

Posted 12 November 2014 - 10:37 PM

Your problem is here:
for (i = square.length -j ; i > 0; --i)
069	            total+=square[i][j];



I'm betting j == 0. If that's the case, the problem is obvious, as arrays are indexed from 0, ..., length-1.

Quote

not sure why the second code didnt post properly. Where is the edit button???

There is a post minimum on the edit button. Don't worry- you're almost there. If your edit time runs out (once you get edit permissions), you can always report your post so a moderator can fix the issue. The report button notifies us immediately of the issue. And usually, someone is online to take care of it.
Was This Post Helpful? 0
  • +
  • -

#3 JavaNewbie82   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 21
  • Joined: 07-October 14

Re: Can not figure out what is going on with my MagicSquare Program

Posted 12 November 2014 - 10:45 PM

View Postmacosxnerd101, on 12 November 2014 - 10:37 PM, said:

Your problem is here:
for (i = square.length -j ; i > 0; --i)
069	            total+=square[i][j];



I'm betting j == 0. If that's the case, the problem is obvious, as arrays are indexed from 0, ..., length-1.

Quote

not sure why the second code didnt post properly. Where is the edit button???

There is a post minimum on the edit button. Don't worry- you're almost there. If your edit time runs out (once you get edit permissions), you can always report your post so a moderator can fix the issue. The report button notifies us immediately of the issue. And usually, someone is online to take care of it.


Thanks for fixing the code post.

I changed the program as you suggested but I am still getting the same error message.

******** Square 1 ********
8 1 6
3 5 7
4 9 2
The sum of the row 0 is: 15
The sum of the row 1 is: 15
The sum of the row 2 is: 15
The sum of the column 0 is: 15
The sum of the column 1 is: 15
The sum of the column 2 is: 15
The sum of the main diagonal is: 15
The sum of the other diagonal is: 15
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Square.sumColumn(Square.java:47)
at Square.isMagic(Square.java:89)
at SquareTest.main(SquareTest.java:50)
public int sumMainDiag()
    {
        total = 0;
        for (i = 0, j = square.length - 1; i < square.length; i++)
        {
        	total += square[i][j];
        	j--;
        }
        return total;
    }

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

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Can not figure out what is going on with my MagicSquare Program

Posted 12 November 2014 - 10:47 PM

Your problem is at this section. Notice it is at a different, unrelated spot. I'd guess there is an issue with column here, since i appears to be appropriately dimensioned.
for(i = 0; i < square.length; i++)
047	            total += square[i][column];



Notice that the stack trace points to the sumColumn() method, not the updated methods you posted.
Was This Post Helpful? 0
  • +
  • -

#5 JavaNewbie82   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 21
  • Joined: 07-October 14

Re: Can not figure out what is going on with my MagicSquare Program

Posted 12 November 2014 - 10:53 PM

oh yea it did give a different section. I do not see any problems with the code though.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Can not figure out what is going on with my MagicSquare Program

Posted 12 November 2014 - 10:55 PM

This is where you have to do some debugging by checking the value of column each time this method is invoked. Does column reach a point where column >= square[i].length? If so, then you will have to find out where that happens and fix it.
Was This Post Helpful? 0
  • +
  • -

#7 JavaNewbie82   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 21
  • Joined: 07-October 14

Re: Can not figure out what is going on with my MagicSquare Program

Posted 12 November 2014 - 11:14 PM

hmm...I do not see the problem in the text or code. Ill just have to ask the professor tomorrow. Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Can not figure out what is going on with my MagicSquare Program

Posted 12 November 2014 - 11:17 PM

The code traces back to your isMagic() method. Is i dimensioned on the number of rows or number of columns?
public boolean isMagic(){
079	        int save = sumMainDiag();
080	        for (i = 0; i < square.length; ++i){
081	            if(save!= sumRow(i))
082	                return false;
083	            if(save != sumColumn(i))
084	                return false;
085	        }
086	        if(save != sumOtherDiag())
087	            return false;
088	        return false;
089	    }



Quote

hmm...I do not see the problem in the text or code.

Reading the error message is actually an important skill. The second line at Square.sumColumn(Square.java:47) tells you where to start. The third line tells you the isMagic() method invoked the sumColumn() method at line 89. Then it traces back to main() at line 50. Java error messages are quite friendly in this manner, telling you where to look.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Square.sumColumn(Square.java:47)
at Square.isMagic(Square.java:89)
at SquareTest.main(SquareTest.java:50)


Was This Post Helpful? 0
  • +
  • -

#9 JavaNewbie82   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 21
  • Joined: 07-October 14

Re: Can not figure out what is going on with my MagicSquare Program

Posted 12 November 2014 - 11:49 PM

ok, I have no idea what happened but I decided to delete the code all together and redo it and it worked perfectly this time. The code is the exact same. I think it had to do with the i as you stated. Thanks for the help!!!
public boolean isMagic(){
    	int save = sumMainDiag();
    	for (int i = 0; i < square.length; ++i){
    		if(save != sumRow(i))
    			return false;
    		if(save != sumColumn(i))
    			return false;
    	}
    	if(save != sumOtherDiag())
    		return false;
		return true;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1