Implement matrix reading.
This commit is contained in:
@@ -1,48 +1,47 @@
|
|||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.FileNotFoundException;
|
import java.util.Random;
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
int[][] population = new int[9][9]; // Матрица для хранения всего Sudoku
|
||||||
char[][] matrix = readInputs();
|
Random random = new Random();
|
||||||
|
|
||||||
for (char[] row : matrix) {
|
public static void main(String[] args) {
|
||||||
for (char c : row) {
|
Main main = new Main();
|
||||||
System.out.print(c + " ");
|
main.generateInitialMatrix();
|
||||||
|
main.printPopulation();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Генерация начальной матрицы из файла
|
||||||
|
public void generateInitialMatrix() {
|
||||||
|
String inputFileName = "input.txt";
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(inputFileName))) {
|
||||||
|
String line;
|
||||||
|
int rowIndex = 0; // Индекс строки в матрице
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
String[] tokens = line.split(" "); // Разделяем строку по пробелам
|
||||||
|
for (int colIndex = 0; colIndex < tokens.length; colIndex++) {
|
||||||
|
if (tokens[colIndex].equals("-")) {
|
||||||
|
population[rowIndex][colIndex] = random.nextInt(10); // Генерируем случайное число
|
||||||
|
} else {
|
||||||
|
population[rowIndex][colIndex] = Integer.parseInt(tokens[colIndex]); // Конвертируем строку в число
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rowIndex++; // Переход к следующей строке
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error reading the file: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Вывод содержимого population для проверки
|
||||||
|
public void printPopulation() {
|
||||||
|
System.out.println("Generated Matrix:");
|
||||||
|
for (int[] row : population) {
|
||||||
|
for (int num : row) {
|
||||||
|
System.out.print(num + " ");
|
||||||
}
|
}
|
||||||
System.out.println();
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user