There are a few of these topics out there, but this problem has a slight twist that makes it different.
I'm focused on only half of a larger problem. I'm sure many of you are aware of the magic square problem.
Prompt:
Assume a file with lines and numbers on each line like the square shown. Write a program that reads info into a two dimensional array of intS. The program should determine if the matrix is a magic square or not.
Working Solution:
public static int[][] create2DIntMatrixFromFile(String filename) throws Exception {
int[][] matrix = {{1}, {2}};
File inFile = new File(filename);
Scanner in = new Scanner(inFile);
int intLength = 0;
String[] length = in.nextLine().trim().split("\s+");
for (int i = 0; i < length.length; i++) {
intLength++;
}
in.close();
matrix = new int[intLength][intLength];
in = new Scanner(inFile);
int lineCount = 0;
while (in.hasNextLine()) {
String[] currentLine = in.nextLine().trim().split("\s+");
for (int i = 0; i < currentLine.length; i++) {
matrix[lineCount][i] = Integer.parseInt(currentLine[i]);
}
lineCount++;
}
return matrix;
}
public static boolean isMagicSquare(int[][] square) {
return false;
}
Here is my (old) code for reading info from a text file into a 2D array:
public static int[][] create2DIntMatrixFromFile(String filename) throws Exception {
int[][] matrix = {{1}, {2}};
File inFile = new File(filename);
Scanner in = new Scanner(inFile);
in.useDelimiter("[/n]");
String line = "";
int lineCount = 0;
while (in.hasNextLine()) {
line = in.nextLine().trim();
Scanner lineIn = new Scanner(line);
lineIn.useDelimiter("");
for (int i = 0; lineIn.hasNext(); i++) {
matrix[lineCount][i] = Integer.parseInt(lineIn.next());
lineIn.next();
}
lineCount++;
}
return matrix;
}
public static boolean isMagicSquare(int[][] square) {
return false;
}
And here is the text file I am reading from. It is in the shape of a 9x9 2D array, but the program must accommodate an array of ambiguous size.
37 48 59 70 81 2 13 24 35
36 38 49 60 71 73 3 14 25
26 28 39 50 61 72 74 4 15
16 27 29 40 51 62 64 75 5
6 17 19 30 41 52 63 65 76
77 7 18 20 31 42 53 55 66
67 78 8 10 21 32 43 54 56
57 68 79 9 11 22 33 44 46
47 58 69 80 1 12 23 34 45
There are two spaces proceeding each line on purpose.
Before I state the exact problem, this is a homework template so the method declaration and variable initialization was pre-determined.
I'm not positive that the method even correctly creates a 2D Array from the file because I can't run it yet. The issue is that for some reason "matrix" was initialized with 1 column and 2 rows. For what reason I'm not sure, but in order to fill an array with the numbers from the file I need to create a 2D array with dimensions equal to the number of values in a line.
I previously had written code to create a new 2D array
int[line.length()][line.length()]
but it created a 36x36 array because that's how many individual characters are in one line. I have a feeling it's as simple as looping through the first line and having a counter keep track of each sequence of numbers separated by a zero.
To me, that solution seems too inefficient and time consuming just to find the dimensions of the new array. What's the best way to accomplish this? Without using ArrayLists as I have to rewrite this program after using ArrayLists.
See Question&Answers more detail:
os