Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A magic square is a square filled with numbers so that the total of each row, each column and each main diagonal are all the same. What should I do to resolve this? What I am require to do is write a program that will read multiple sets of 16 numbers from Lab8Data.txt(these numbers fill a 4x4 square) and determine if each set of 16 numbers is a magic square and use the sentinel method to control your loop. The sentinel value is -999.Lab8Data.txt is below the code.

When I run the program it shows these errors.

The errors are Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Main.main(Main.java:22)

Java
  1  import java.io.File;
  2  
  3  import java.io.IOException;
  4  
  5  import java.util.Scanner;
  6  
  7  class magics
  8  
  9  {
 10  
 11      public static void main(String[] args) throws IOException
 12  
 13      {
 14  
 15          File data = new File("Lab8Data.txt");
 16          Scanner input = new Scanner(data);
 17          int[][] matrix = new int[4][4];
 18          for(int row=0; row<4; row++)
 19              matrix[0][row] = input.nextInt();
 20          while(matrix[0][0] != -999) {
 21              for(int row=1; row < matrix.length; row++)
 22                  for(int column=0; column< matrix.length; column++)
 23                      matrix[column][column] = input.nextInt();
 24              for (int[] ints : matrix) {
 25                  for (int row = 0; row < matrix.length; row++)
 26                      System.out.print(ints[row] + " ");
 27                  System.out.println();
 28              }
 29  
 30              int[] rowTotal = new int[4];
 31  
 32              for (int[] value : matrix)
 33                  for (int column = 0; column < matrix.length; column++)
 34                      rowTotal[column] += value[column];
 35  
 36              int[] columnTotal = new int[4];
 37  
 38              for(int row=0; row < matrix.length; row++)
 39  
 40                  for (int[] Matrix : matrix) columnTotal[row] += Matrix[row];
 41  
 42  
 43              int diagonalOne = 0;
 44  
 45              for(int row = 0; row < matrix.length; row++)
 46                  diagonalOne = diagonalOne + matrix[row][row];
 47              int otherDiagonal = 0;
 48              for (int row = 0; row < matrix.length; row++) {
 49                  otherDiagonal = otherDiagonal + matrix[row][Math.abs(3 - row)];
 50              }
 51              int row = rowTotal[0];
 52              boolean rowSum = true;
 53              for(int column = 0; column < matrix.length; column++)
 54                  if (rowTotal[column] != row) {
 55                      rowSum = false;
 56                      break;
 57                  }
 58              int c = columnTotal[0];
 59              boolean isMagic = true;
 60              for (int column = 0; column<matrix.length; column++)
 61                  if (rowTotal[column] != column) {
 62                      isMagic = false;
 63                      break;
 64                  }
 65              int diagonal = diagonalOne;
 66              boolean diagonalSum = otherDiagonal == diagonal;
 67              boolean squareIsAMagicSquare = false;
 68              if(rowSum && isMagic && diagonalSum)
 69                  if(row == c && c == diagonal)
 70                      squareIsAMagicSquare = true;
 71              if(squareIsAMagicSquare)
 72                  System.out.println("Is a magic square");
 73              else
 74                  System.out.println("NOT a magic square");
 75  
 76          }
 77  
 78      }
 79  
 80  }


What I have tried:

I don't know what to do. The following is how the code should be like.

Lab8Data.txt sample data: (the actual file will contain more than two sets of 16 numbers) This is what I need after it run. I need it to print out 4 rows of numbers and display if it is a magic square or not.
1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

1 15 14 4

12 6 7 9

8 10 11 5

13 3 2 16

-999


Sample run:

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

NOT a magic square

1 15 14 4

12 6 7 9

8 10 11 5

13 3 2 16

IS a magic square

Lab8Data.txt file is below 
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
1 15 14 4
12 6 7 9
8 10 11 5
13 3 2 16
30 8 20 11
3 10 21 35
24 25 13 7
12 26 15 16
14 8 19 92
37 53 16 27
67 10 54 2
15 62 44 12
2 5 6 1
8 5 2 9
4 5 6 7
3 2 7 5
-999 -999 -999 -999
Posted
Updated 27-Oct-22 20:48pm
v2
Comments
Richard Deeming 27-Oct-22 3:29am    
matrix[column][column] = ...

Why are you only setting the diagonal elements? Are you sure you shouldn't be using:
matrix[row][column] = ...

1 solution

NoSuchElementException is thrown because Scanner input.nextInt() is attempting to read more values from the file Lab8Data.txt than exists in the file. Your code is obviously not reading the values as you expect. Notice that when you emit the matrix just read in it does not match the data in the file. Even after fixing the matrix[column][column] = ... line mentioned by Richard Deeming.

I believe it's time to break out the debugger so you can inspect the values as they are read and also the values of the matrix after it has been read in. When you emit the matrix back out I think you'll see that it is being output with rows swapped for columns when compared to the values as shown in the file. That's probably not an issue as long as you keep the rwos and columns consistent in the matrix, the processing and your mind. This may have occurred because the line for (int[] ints : matrix) may process the matrix values differently than you expected. Again the debugger will help to determine what is actually happening vs what you meant to have happen. It is an invaluable tool and the sooner you learn to use it the better and more productive your programming life will be.

I also noticed that in your code you read the 1st 4 values into your array then you enter a while loop that will terminate on the sentinel (-999). That loop reads the next 12 values which are then processed to determine if the matrix is magic. Then you loop back to read in the next matrix values - but the first set of 4 values is not read in - that loop is outside the while. So you read in the wrong number of values and process for magic on a mixture of values from this and the first matrix. Then it loops back to read a 3rd matrix. The sentinel hasn't been read yet so it starts to read the next 12 values but it only finds 5 moire values and so the exception is thrown.

Stepping through the code with a debugger would show all this very quickly. I'm sorry if it seems I am harping on the debugger a lot here but that's only because I am. Learn to use it, use it, and you will retain your sanity (or least more of it). And, you'll most likely finish your programming tasks much faster.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900