1

I'm trying to load file, which looks like this:

0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 

to 2d array, which is in class sudokuboard.

The function, which i use for this is following:

 @Override
public SudokuBoard read() throws DaoException {

    SudokuBoard board = new SudokuBoard();

    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));

        while (in.ready() == true) {

            for (int row = 0; row < 9; row++) {
                for (int column = 0; column < 9; column++) {

                    board.setDigit(row, column, in.read());

                    //System.out.print(in.read() + ", ");
                }
                in.read(); // this is important

                System.out.println("");
            }
        }
    } catch (IOException e) {
        throw new DaoException(DaoException.IO_EXCEPTION, e);
    }
    return board;

}

What i have after reading this file is:

0 0 0 6 115 113 0 126 0 
0 0 0 4 115 113 0 126 0 
0 0 0 2 115 113 0 126 0 
0 0 0 3 115 113 0 126 0 
0 0 0 8 115 113 0 126 0 
0 0 0 5 115 113 0 126 0 
0 0 0 1 115 113 0 126 0 
0 0 0 9 120 120 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 

Anybody knows where the problem is ?

Thanks for help !

4
  • Did you bother to read the docs for BufferedReader.read()? docs.oracle.com/javase/7/docs/api/java/io/… And you can read about ready() method while you are there. Commented May 18, 2014 at 12:52
  • 1
    I would suggest to you to read each line using the method readline of the BufferedReader class and then make a split using the space character in order to obtain an array of characters that you can convert in an array of integer. Commented May 18, 2014 at 12:52
  • @AlessandroSuglia actualy i tried that way, but there were so many errors, that i passed ;p Commented May 18, 2014 at 12:56
  • Update your code in order to let us to understand what are you doing ;) Commented May 18, 2014 at 13:06

2 Answers 2

1

What do you think about this solution?

public static int[][] readBoard(String boardFileName) throws IOException {
    int[][] board = new int[9][9];

    BufferedReader reader = null;

    try {
        reader = new BufferedReader(new FileReader(boardFileName));

        for(int i = 0; i < 9 && reader.ready(); i++) {
            String[] splittedRow = reader.readLine().split(" "); // split using the space character
            for(int j = 0; j < 9; j++) {
                board[i][j] = Integer.parseInt(splittedRow[j]);
            }
        }

        return board;

    } catch(IOException e) {
        throw e; // lets the caller manage the exception
    } finally {
        assert reader != null;
        reader.close();
    }
}

Obviously, don't take care about the error management that is completely absent :)

I hope that this code will help you...

Sign up to request clarification or add additional context in comments.

Comments

0

I've been playing with your code a bit. And this is what I came up with. Surely, there are better solutions, but I think this one will work for you.

BufferedReader in = null;
try {
    in = new BufferedReader(new InputStreamReader(new FileInputStream("C:/Users/Rod/Desktop/123.txt")));
    int column = 0;
    int row = 0;
    int pom;
    while ((pom = in.read()) != -1) {
        pom -= 48;
        if (pom > -1 && pom < 10) {
            System.out.print("" + pom + ", ");
            // board.setDigit(row, column, pom);
            column++;
            if (column > 8) {
                column = 0;
                System.out.println("");
                row++;
                if (row > 8)
                    break;
            }
        }
    }

} catch (Exception e) {
    System.out.print("ERROR");
    // e.printStackTrace();
} finally {
    if (in != null)
        in.close();
}

Tell me did it worked well for you.

P.S. I've edited code after comments. All the best.

4 Comments

This has too many problems to list, but it's still using ready() incorrectly, and still misses EOF.
Thanks for commenting. Will try to improve it and get better. ;)
Did you test this? Hint: do you ever do anything with the computed value of 'pom'? Also: why use FileInputStream when there's FileReader? And what happens if the input file is malformed?
Thank you for viewing code and commenting. Tested it in Eclipse and it's working. OP is using FileInputStream, so my guess is that it is part of his project (will add FileReader version later if needed). Value of pom is to be used by 'board.setDigit(row, column, pom);' function (edited in code). As long as there are 81 digits in .txt file, it will work. My assumption is that text file is given in correct form as OP haven't mention anything about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.