Odczyt z pliku tekstowego z zamianą na tablicę charów.

0

Cześć, mam problem z odczytaniem pliku tekstowego (przykładowego sudoku) i zrobienia z niego tablicy dwuwymiarowej charów.
Przykładowe sudoku:
702050600
000003000
100009500
000000090
043000750
090000008
009700005
000200000
007040203

I chce zrobić z tego tablicę dwuwymiarową charów. Na razie kod klasy wygląda tak:


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class SudokuReader {
    private static final int ROW_SIZE = 9;
    private static final int COLUMN_SIZE = 9;

    private int[][] unsolvedBoard = new int[ROW_SIZE][COLUMN_SIZE];
    //private char[][] readSudoku = new char[ROW_SIZE][COLUMN_SIZE];

     private char[][] readSudoku = {
            {'7', '0', '2', '0', '5', '0', '6', '0', '0'},
            {'0', '0', '0', '0', '0', '3', '0', '0', '0'},
            {'1', '0', '0', '0', '0', '9', '5', '0', '0'},
            {'0', '0', '0', '0', '0', '0', '0', '9', '0'},
            {'0', '4', '3', '0', '0', '0', '7', '5', '0'},
            {'0', '9', '0', '0', '0', '0', '0', '0', '8'},
            {'0', '0', '9', '7', '0', '0', '0', '0', '5'},
            {'0', '0', '0', '2', '0', '0', '0', '0', '0'},
            {'0', '0', '7', '0', '4', '0', '2', '0', '3'}
    };

    public char[][] readFile() {

        return readSudoku;
    }

    public int[][] boardConverter() {
        for (int i = 0; i < readSudoku.length; i++) {
            for (int j = 0; j < readSudoku.length; j++) {
                unsolvedBoard[i][j] = Integer.parseInt(String.valueOf(readSudoku[i][j]));
            }
        }
        return unsolvedBoard;
    }

    public void print() {
        for (int i = 0; i < unsolvedBoard.length; i++) {
            for (int j = 0; j < unsolvedBoard.length; j++) {
                System.out.print(unsolvedBoard[i][j]);
            }
            System.out.println();
        }
    }
}

Pomożecie? :)

0

Napisałem jeszcze teraz coś takiego, ale nie wywietla mi idealnie...

public char[][] readFile() {
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader("example.txt"));
            for (int i = 0; i < readSudoku.length; i++) {
                for (int j = 0; j < readSudoku.length; j++) {
                    char sign = (char) bufferedReader.read();
                    readSudoku[i][j] = sign;
                    System.out.print(readSudoku[i][j]);
                }
                System.out.println();
            }
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return readSudoku;
    }

edit: ok usunąłem prinln na końcu, ale teraz mam problem, bo rzuca mi wyjątkiem...Bez tytułu.jpg

1

Ależ kombinujesz.

        List<List<String>> board = Files.lines(Paths.get("example.txt"))
                .map(line -> Arrays.stream(line.split("")).toList())
                .toList();

I nie, nie chcesz mieć tablicy ani tym bardziej tablicy charów.

0

Bazująć na odpowiedzi @Shalom wystarczy jeszcze zrobić operację flatMap() zeby zredukować to do listy Stringów:

List<String> list = Files.lines(Paths.get("example.txt"))
                .map(line -> Arrays.stream(line.split("")).toList())
                .flatMap(List::stream)
                .collect(Collectors.toList());
0

@SharkSamuraj: @Shalom: ok już ogarnąłem, dzięki za pomoc. ;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1