start implementing read from .txt functionality

This commit is contained in:
emil
2024-11-26 00:49:55 +03:00
parent 24acb8af7b
commit 5483149685
4 changed files with 57 additions and 1 deletions
BIN
View File
Binary file not shown.
+48
View File
@@ -0,0 +1,48 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
char[][] matrix = readInputs();
for (char[] row : matrix) {
for (char c : row) {
System.out.print(c + " ");
}
System.out.println();
}
}
public static char[][] readInputs() {
char[][] matrix = new char[9][9]; // Initialize a 9x9 matrix
try {
File file = new File("./input.txt");
Scanner scanner = new Scanner(file);
int row = 0;
while (scanner.hasNextLine() && row < 9) {
String line = scanner.nextLine();
if (line.length() != 9) {
throw new IllegalArgumentException("Each line in the file must have exactly 9 characters.");
}
for (int col = 0; col < 9; col++) {
matrix[row][col] = line.charAt(col);
}
row++;
}
if (row != 9) {
throw new IllegalArgumentException("The file must contain exactly 9 lines.");
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
return matrix;
}
}
+9
View File
@@ -0,0 +1,9 @@
- 8 - - - - - 9 -
- - 7 5 - 2 8 - -
6 - - 8 - 7 - - 5
3 7 - - 8 - - 5 1
2 - - - - - - - 8
9 5 - - 4 - - 3 2
8 - - 1 - 4 - - 9
- - 1 9 - 3 6 - -
- 4 - - - - - 2 -
-1
View File
@@ -1 +0,0 @@
public