简体   繁体   中英

How to read a txt file from PrintWriter with BufferedReader to read grid coordinates with value?

I'm writing a txt file with some 3 digit numbers (x,y,z) where x is line, y is column and z is value for my sudoku grid.

I've managed to write my txt file where every 3 lines I have a number. Line 1 = x, line 2 = y, line 3 = z and then line 4 = x, line 5 = y and so on...

I'm having trouble writing the part of the code where I read the txt file and then print my values at the right coordinates on the console.

I am sure there are many mistakes in my code. This is the method I made to print on console:

static void imprimerGrille()
{            
    try
    {
        BufferedReader lectureTexte = new BufferedReader(new FileReader("partie.txt"));            
        String ligne = lectureTexte.readLine();
        int count = 0;
        while ((ligne = lectureTexte.readLine()) != null){
            for (int i=0; i<grilleSudoku.length; i++){
                System.out.print("\n");
                if (i%3 == 0) 
                    System.out.println();                
                for (int j=0; j<grilleSudoku.length; j++){
                    if (j%3 == 0) 
                        System.out.print(" ");    
                    if (count%3 == 0){
                        //This would be the value I want in line, column coordinate                       
                    }
                    else if (count%3 == 1){
                       //This is my line coordinate
                    }
                    else if (count%3 == 2){                         //colonne
                       //This is my column coordinate 
                    }

                }                
            }
            count++;
            if (count == 3){
                count = 0;
            }
        }
    } catch(Exception ex){
        System.out.println("Fichier inexistant");
    }
}

I had this code here to print the layout of my sudoku grid with 0's. I'm just having trouble incorporating it with the BufferedReader part.

   /*for (int i=0; i<grilleSudoku.length; i++){
                System.out.print("\n");
                if (i%3 == 0) System.out.println();
            for (int j=0; j<grilleSudoku.length; j++){
                if (j%3 == 0) System.out.print(" ");                    
                for (int x = 0; x<9; x++){
                    if (grilleSudoku[i][j] == x) System.out.print(x);                        
                }                    
            }                    
        } */ 

With the given txt structure you can do something like this

int[][] sudoku = new int[3][3];
try (BufferedReader br = new BufferedReader(new FileReader("<your-input-file>"))) {
    String line;
    int i = 1;
    int row = 0;
    int column = 0;
    while ((line = br.readLine()) != null) {
        if (i++ % 3 == 0) {
            sudoku[row][column++] = Integer.parseInt(line);
            if (column % 3 == 0) {
                ++row;
                column = 0;
            }
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        System.out.print(String.format("%d ", sudoku[i][j]));
    } 
    System.out.println();
}

I recommend to change your txt to this format (not a valid sudoku):

1,2,3
4,5,6
7,8,9

It has the benefit to write cleaner code, readable input, and if you want to change the grid to 4x4, you only need to change the input file and the 2 dimensional array. (If you use an ArrayList, then only the file needs to be modified)

int[][] sudoku = new int[3][3];
try (BufferedReader br = new BufferedReader(new FileReader("<your-input-file>"))) {
    String line;
    int row = 0;
    while ((line = br.readLine()) != null) {
        String[] splitted = line.split(",");
        for (int column = 0; column < splitted.length; column++) {
            sudoku[row][column] = Integer.parseInt(splitted[column]);
        }
        row++;
    }
} catch (IOException e) {
    e.printStackTrace();
}
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        System.out.print(String.format("%d ", sudoku[i][j]));
    }
    System.out.println();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM