It's hardly worth working over a text file for two small integer numbers so we should assume that the text file could also potentially hold something like:
342354645686786
423423689909754
677554345952536
238884546869621
345665678683481
342354645686786
423423689909754
677554345952536
238884546869621
345665678683481
342354645686786
423423689909754
677554345952536
238884546869621
423423689909754
Perhaps we may never know exactly what is in the file other than it contains several lines of digits. With this mind, don't try to initially fill an int[][] Array with the digits in file since we may never know what to initialize the 2D Array with, instead use an ArrayList or List Interface to store an int[] array for each line as they are read in because they can grow dynamically where as an Array can not. Each int[] array contained within the List can then be placed into the matrix (2D) Array once all the data is collected, for example:
public static int[][] readMatrix(String url) throws FileNotFoundException {
File file = new File((url));
java.util.List<int[]> listOfArrays = new java.util.ArrayList<>();
String line;
// 'Try With Resources' used here to auto-close the reader and free resources.
try (Scanner sc = new Scanner(file)) {
while (sc.hasNextLine()) {
line = sc.nextLine().trim();
/* Line Validation:
Skip blank lines (if any) or any line that does
not contain all digits. */
if (line.isEmpty() || !line.matches("\\d+")) {
continue;
}
/* Declare and initialize an int[] Array to the
number of dogot in line. */
int[] array = new int[line.length()];
for (int i = 0; i < line.length(); i++) {
array[i] = Integer.parseInt(String.valueOf(line.charAt(i)));
}
// Add the newly created array into the List.
listOfArrays.add(array);
}
}
/* Now that the file is all read in, we can create
the matrix. Don't supply an initialization value
for the inner arrays because for all we know this
might end up being a stagarded matrix. It doesn't
need a value. */
int[][] matrix = new int[listOfArrays.size()][];
/* Iterate through the list and apply the int[]
arrays in the list to the matrix[][]. Remember,
a 2D Array is an Array of Arrays in Java. */
for (int i = 0; i < listOfArrays.size(); i++) {
matrix[i] = listOfArrays.get(i);
}
return matrix; // Return the created matrix.
}
To run this method against the file data shown above you might do something like this:
int[][] matrix = null;
try {
matrix = readMatrix(url);
}
catch (Exception ex) {
System.err.println(ex.getMessage());
}
if (matrix != null) {
for (int[] ary : matrix) {
System.out.println(Arrays.toString(ary).replaceAll("[\\[\\],]", ""));
}
}
And the Console Window would display:
3 4 2 3 5 4 6 4 5 6 8 6 7 8 6
4 2 3 4 2 3 6 8 9 9 0 9 7 5 4
6 7 7 5 5 4 3 4 5 9 5 2 5 3 6
2 3 8 8 8 4 5 4 6 8 6 9 6 2 1
3 4 5 6 6 5 6 7 8 6 8 3 4 8 1
3 4 2 3 5 4 6 4 5 6 8 6 7 8 6
4 2 3 4 2 3 6 8 9 9 0 9 7 5 4
6 7 7 5 5 4 3 4 5 9 5 2 5 3 6
2 3 8 8 8 4 5 4 6 8 6 9 6 2 1
3 4 5 6 6 5 6 7 8 6 8 3 4 8 1
3 4 2 3 5 4 6 4 5 6 8 6 7 8 6
4 2 3 4 2 3 6 8 9 9 0 9 7 5 4
6 7 7 5 5 4 3 4 5 9 5 2 5 3 6
2 3 8 8 8 4 5 4 6 8 6 9 6 2 1
3 4 5 6 6 5 6 7 8 6 8 3 4 8 1