简体   繁体   English

将文本文件放入二维数组(数独网格)

[英]Put text file to a 2D Array (Sudoku Grid)

I need to load a.txt file with int like 001 012 017 108 etc. -> 001 = means column 0, row 0, value 1. This is the code I have so far, the issue is finding a way to remove the space which are read as 0 and also print a new line for every rows.我需要加载带有int的a.txt文件,例如001 012 017 108等-> 001 =表示第0列,第0行,值1。这是我到目前为止的代码,问题是找到一种删除空间的方法它们被读取为 0 并且还为每一行打印一个新行。 Thank you: Code so far: The output is.谢谢:到目前为止的代码:output 是。 12345678023456789034567891045678912056789123067891234078912345089123456091234567Exception in thread "main" java.lang:ArrayIndexOutOfBoundsException: -49 12345678023456789034567891045678912056789123067891234078912345089123456091234567线程“主”中的异常 java.langs:ArrayIndexOutOfBound:-4

public static void main (String [] args) {
    int[][] sudokuGrid = new int [9][9];

    try {
        FileReader fichierALire = new FileReader("partie1.txt");
        int c = fichierALire.read();
        while (c != -1){
            int row = fichierALire.read();
            int value = fichierALire.read();
            fichierALire.read();
            sudokuGrid [c-48][row-48] = value-48;
            c = fichierALire.read();
            System.out.print(sudokuGrid[c-48][row-48]);
        }

    } catch (IOException exception) {
        System.out.println("Il y a une erreur lors de la lecture: " + exception.getMessage());
    }
}

} }

Read the data line by line (instead of character by character).逐行读取数据(而不是逐个字符)。

the issue is finding a way to remove the space问题是找到删除空间的方法

Then as you read each line you can use the String.split(..) method.然后,当您阅读每一行时,您可以使用String.split(..)方法。

It will then return you an Array of your values.然后它将返回您的值数组。 Then you can iterate through each entry in the array and parse the value and populate your grid.然后您可以遍历数组中的每个条目并解析值并填充您的网格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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