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)
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