Results 1 to 7 of 7
  1. #1
    bengiles89 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default Magic Squares, input confusion

    Hello, I'm running into a problem with my fourth homework assignment for CS201. Yes, my question involves homework, but I'm not asking for code.

    I can't figure out how to read the magicData file. I'm using Eclipse, and while I believe it should be in the project's main directory, I've also placed copies in the src and bin folders. So, I don't believe the error is a result of the file being misplaced or incorrectly named (the file has no extension).

    Here's my driver program:
    Java Code:
    // ****************************************************************
    // SquareTest.java
    //
    // Uses the Square class to read in square data and tell if
    // each square is magic.
    //
    // ****************************************************************
    
    import java.util.Scanner;
    import java.io.IOException;
    import java.io.File;
    
    public class SquareTest {
    	public static void main(String[] args) throws IOException {
    		Scanner scan = new Scanner(new File("magicData"));
    		// make sure that the file magicData is in the current directory
    
    		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 s = new Square(size);
    
    			// call its read method to read the values of the square
    			System.out.println("\n***** Square " + count + " *****");
    			
    			// print the square
    			s.printSquare();
    			
    			// print the sums of its rows
    			for (int row = 0; row < size; row++) {
    				System.out
    				.println("Sum of row " + row + "is: " + s.sumRow(row));
    			}
    			System.out.println();
    			
    			// print the sums of its columns
    			for (int col = 0; col < size; col++) {
    				System.out.println("Sum of columns " + col + " is: "
    						+ s.sumCol(col));
    			}
    			System.out.println();
    			
    			// print the sum of the main diagonal
    			System.out.println("The sum of the main diagonal is: "
    					+ s.sumMainDiag());
    			System.out.println();
    			
    			// print the sum of the other diagonal
    			System.out.println("The sum of the other diagonal is: "
    					+ s.sumOtherDiag());
    			System.out.println();
    			
    			// determine and print whether it is a magic square
    			System.out.println("Is it a magic square: " + s.magic());
    
    			// get size of next square
    			size = scan.nextInt();
    			count++;
    		}
    	}
    }
    Here's my class file:
    Java Code:
    // ****************************************************************
    // 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]; // table user defined
    	}
    
    	// --------------------------------------
    	// 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 = 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.length; row++) {
    			sum = 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 = sum + square[j][j];
    		}
    		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 = 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 row = 0; row < square.length; row++) {
    				if (sum != sumRow(row)) {
    					answer = false;
    				}
    			}
    			for (int col = 0; col < square.length; col++) {
    				if (sum != sumCol(col)) {
    					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 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] + " ");
    				System.out.println();
    			}
    	}
    
    }
    And here's the magicData file.
    Java Code:
    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
    Also, here's the frustrating output I'm currently receiving:
    Java Code:
    0 
    0 
    0 
    0 
    0 
    etc...
    
    Sum of row 0is: 0
    Sum of row 1is: 0
    Sum of row 2is: 0
    Sum of row 3is: 0
    Sum of row 4is: 0
    Sum of row 5is: 0
    Sum of row 6is: 0
    Sum of row 7is: 0
    Sum of row 8is: 0
    Sum of row 9is: 0
    Sum of row 10is: 0
    Sum of row 11is: 0
    Sum of row 12is: 0
    Sum of row 13is: 0
    Sum of row 14is: 0
    Sum of row 15is: 0
    Sum of row 16is: 0
    Sum of row 17is: 0
    Sum of row 18is: 0
    Sum of row 19is: 0
    Sum of row 20is: 0
    Sum of row 21is: 0
    Sum of row 22is: 0
    Sum of row 23is: 0
    
    Sum of columns 0 is: 0
    Sum of columns 1 is: 0
    Sum of columns 2 is: 0
    Sum of columns 3 is: 0
    Sum of columns 4 is: 0
    Sum of columns 5 is: 0
    Sum of columns 6 is: 0
    Sum of columns 7 is: 0
    Sum of columns 8 is: 0
    Sum of columns 9 is: 0
    Sum of columns 10 is: 0
    Sum of columns 11 is: 0
    Sum of columns 12 is: 0
    Sum of columns 13 is: 0
    Sum of columns 14 is: 0
    Sum of columns 15 is: 0
    Sum of columns 16 is: 0
    Sum of columns 17 is: 0
    Sum of columns 18 is: 0
    Sum of columns 19 is: 0
    Sum of columns 20 is: 0
    Sum of columns 21 is: 0
    Sum of columns 22 is: 0
    Sum of columns 23 is: 0
    
    The sum of the main diagonal is: 0
    
    The sum of the other diagonal is: 0
    
    Is it a magic square: true
    
    etc...
    Any ideas as to why my program currently disregards the contents of magicData?

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    to make sure where your file is being read from, do this:

    System.out.println (fileObject.getCanonicalPath());

  3. #3
    bengiles89 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the quick reply, but I'm afraid I don't understand.

    Should I place "System.out.println (fileObject.getCanonicalPath());" in the driver program? Does it matter where? Do I need to import any java.util's?

    Also, what does that mean? Won't I need to declare a local variable named fileObject?

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    do this: instead of
    Java Code:
    Scanner scan = new Scanner(new File("magicData"));
    do

    Java Code:
    File theFile = new File("magicData");
    try{
      System.out.println (theFile.getCanonicalPath());
    } catch (Exception ex) {
      // ignore
    }
    Scanner scan = new Scanner(theFile);

  5. #5
    bengiles89 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    Thanks! I replaced the code, but unfortunately, I don't see any difference in my output.

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You haven't wrapped your File() call in a try block, so you would definitely know if the program were not finding the file -- you'd get a FileNotFoundException and a System.exit(). (Delete all copies of the file and try it to see what that looks like.) The problem is that you are never reading and filling in the values of the cells in your squares. You only have two scan.nextInt() calls, and they are both trying to get sizes.

    -Gary-

  7. #7
    bengiles89 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    Ah, awesome. Thanks, I'll get on that and if I have any more problems I'll post the updated code.

Similar Threads

  1. Magic squares LUX method
    By gandalf5166 in forum New To Java
    Replies: 14
    Last Post: 04-17-2010, 04:30 PM
  2. Calculating Squares and Cubes in a table
    By aldorfski_17 in forum New To Java
    Replies: 4
    Last Post: 03-22-2010, 07:17 PM
  3. Magic Square!!!... :D
    By joms999 in forum New To Java
    Replies: 4
    Last Post: 02-25-2010, 07:55 AM
  4. help with perfect squares
    By AmplifiedKid in forum New To Java
    Replies: 1
    Last Post: 09-19-2009, 07:44 PM
  5. Perfect Squares
    By divyachaparala in forum New To Java
    Replies: 4
    Last Post: 02-05-2008, 09:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •