简体   繁体   中英

Read file.txt and take the int value in it then put them into a 2D array for sudoku

I need to be able to download a sudoku file with int values like this one:

partie1.txt {001 012 023 034 045 056 067 078 089 102 113 124 135 146 157 168 179 181 203 214 225 236 247 258 269 271 282 304 315 326 337 348 359 361 372 383 405 416 427 438 449 451 462 473 484 506 517 528 539 541 552 563 574 585 607 618 629 631 642 653 664 675 686 708 719 721 732 743 754 765 776 787 809 811 822 833 844 855 866 877 888}

and be able to use it in a sudoku game.

How do I download a file in string, convert it to ints, and put those values into a 2D array?

import java.io.*;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws IOException {
        File FichierALire1 = new File("C:\\Users\\chris\\partie1.txt");
        File FichierALire2 = new File("C:\\Users\\chris\\partie2.txt");
        File FichierALire3 = new File("C:\\Users\\chris\\partie3.txt");

        try (
                FileReader unFichier1 = new FileReader(FichierALire1);
                BufferedReader leBuffer1 = new BufferedReader(unFichier1);
                FileReader unFichier2 = new FileReader(FichierALire2);
                BufferedReader leBuffer2 = new BufferedReader(unFichier2);
                FileReader unFichier3 = new FileReader(FichierALire3);
                BufferedReader leBuffer3 = new BufferedReader(unFichier3);
        ) {
            // Nous avons les fichiers partie1.txt,
            // partie2.txt et partie3.txt.
            System.out.println("ligne: " + leBuffer1.readLine());
            System.out.println("ligne: " + leBuffer2.readLine());
            System.out.println("ligne: " + leBuffer3.readLine());
            System.out.println(numbers);
        } catch (FileNotFoundException exception) {
            System.out.println(" Fichier introuvable!");
        }
    }
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class Main {
    static Integer[][] convertStringTo2DArray(String s) {
        String[] sArray = s.split(" ");
        Integer[][] intArray = new Integer[sArray.length][3];
        for (int i = 0; i < sArray.length; i++) {
            for (int j = 0; j < 3; j++) {
                intArray[i][j] = Integer.parseInt(sArray[i].split("")[j]);
            }
        }
        return intArray;
    }

    public static void main(String[] args) throws IOException {
        File FichierALire1 = new File("C:\\Users\\amish\\Desktop\\partie.txt");
        try (BufferedReader leBuffer1 = new BufferedReader(new FileReader(FichierALire1));) {
            StringBuilder everything = new StringBuilder();
            String line;
            while ((line = leBuffer1.readLine()) != null) {
                everything.append(line);
            }
            String partie = everything.toString();
            partie = partie.trim();
            System.out.println(Arrays.deepToString(Main.convertStringTo2DArray(partie)));
            leBuffer1.close();
        } catch (FileNotFoundException exception) {
            System.out.println(" Fichier introuvable!");
        }
    }
}

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