I have been attempting to save a 9x9 2D array of ints from a .txt file. I have been on this website for a very long time attempting to get this to work and after doing a ton of tweaking, I am very close to getting it. The only problem is that nothing saves to the array!
Here is my code:
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
public class test {
public static void main(String[] args) throws IOException {
int[][] thing = new int[9][9];
int row = 0;
int rows = 9;
int columns = 9;
File fin = new File("C:\\Users\\David\\workspace\\tester\\initial.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(fin));
String line = reader.readLine();
int lineNum = 0;
while (line != null) {
lineNum++;
System.out.println("line " + lineNum + " = " + line);
line = reader.readLine();
String [] tokens = line.split(",");
for (int j=0; j<tokens.length; j++) {
System.out.println("I am filling the row: " + row);
thing[row][j] = Integer.parseInt(tokens[j]);
}
row++;
}
System.out.println("I am printing the array for testing purposes: ");
for (int i=0; i < rows; i++) {
for (int j = 0; j < columns; j++)
System.out.print(thing[i][j]);
System.out.println("");
}
} catch (IOException error) {
} finally {
if (reader != null) reader.close();
}
}
}
I should say that I am doing this as a test for a sudoku game I am trying to create as a mere side project, I am just super frustrated.
This was also my first post on this site so go easy on me for the formatting. Thanks all!
Edit: I made the change codaddict told me too and now I get the output:
line 1 = 5 3 0 0 7 0 0 0 0
I am filling the row: 0
Exception in thread "main" java.lang.NumberFormatException: For input string: "6 0 0 1 9 5 0 0 0"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at test.main(test.java:36)
Serializable