读取 file.txt 并取其中的 int 值,然后将它们放入 2D 数组中进行数独
Read file.txt and take the int value in it then put them into a 2D array for sudoku
我需要能够下载一个像这样的 int 值的数独文件:
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}
并能够在数独游戏中使用它。
如何下载字符串文件,将其转换为整数,并将这些值放入二维数组中?
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!");
}
}
}
我需要能够下载一个像这样的 int 值的数独文件:
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}
并能够在数独游戏中使用它。
如何下载字符串文件,将其转换为整数,并将这些值放入二维数组中?
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!");
}
}
}