简体   繁体   English

矩阵存储。 我们要去哪里?

[英]Matrix store. Where are we going to do?

We're trying to read a file that contains a sudoko plate with integers and dots for the empty spaces. 我们正在尝试读取一个文件,该文件包含一个sudoko板,其中的整数和点表示空白。 We have no problem reading the file using scanner and new File , but when we can't print the plate in a 9 x 9 matrix. 使用scannernew File读取文件没有问题,但是当我们无法以9 x 9矩阵打印印版时。

We wish to load the file and store it in an 9 x 9 matrix with a while loop. 我们希望加载文件并将其存储在带有while循环的9 x 9矩阵中。 But we get the following error when doing so: 但是这样做时会出现以下错误:

Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Sodoku.main(Sodoku.java:19)

And were not sure, what we're doing wrong. 而且不确定,我们在做什么错。 So far we have the following code: 到目前为止,我们有以下代码:

import java.util.*;
import java.lang.*;
import java.io.*;

public class Sodoku{
    public static void main(String[] args) 
        throws java.io.FileNotFoundException{
        Scanner input = new Scanner (new File("Sudoku.txt"));
        int m = 9;
        int n = 9;
        int[][] a = new int [m][n];
        while (input.next()!=null){
            for (int i=0;i<m;i++){
                for (int j=0;j<n;j++)
                    a[i][j]= input.nextInt();
            }   

        }
        //print the input matrix
        System.out.println("The input sorted matrix is : ");
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++)
                System.out.println(a[i][j]);
        }

    }

}

By using input.next in the while you move the pointer(you consume one of the values), you can use 通过在移动指针时使用input.next(消耗其中一个值),可以使用

while (input.hasNext()==true){

This will not move the pointer it will only verify it has a "next" value 这不会移动指针,只会验证它具有“下一个”值

The best method i can think of would be to load the file as an string and then parse it to the desired format. 我能想到的最好的方法是将文件加载为字符串,然后将其解析为所需的格式。 Here you have the code. 这里有代码。 Not pretty, but will do the trick: 不漂亮,但是会成功的:

import java.util.*;
import java.lang.*;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Sudoku {

    static String readFile(String path, Charset encoding) 
    throws IOException 
    {
        byte[] encoded = Files.readAllBytes(Paths.get(path));
        return new String(encoded, encoding);
    }

    static int[][] parse(String path) throws IOException {
        String input = readFile(path, Charset.defaultCharset());
        int [][] array = new int[10][10];
        int x = 0;
        int y = 0;
        char[] chars = input.toCharArray();
        for (int i = 0; i < input.length(); i++) {
            if (chars[i] == '\n') {
                x = 0;
                y++;
                continue;
            } else if (chars[i] == '.') {
                array[y][x] = -1;
                x++;
                continue;
            }
            array[y][x] = Character.getNumericValue(chars[i]);
            x++;
        }

        return array;
    }
    public static void main(String[] args) 
        throws IOException {
        int m = 9;
        int n = 9;
        int[][] a = parse("Sudoku.txt");
        //print the input matrix
        System.out.println("The input sorted matrix is : ");
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++) {
                if (j==3 || j==6) System.out.print ("| ");
                if (a[i][j]==-1) {
                    System.out.print(". ");
                } else {
                    System.out.print(a[i][j]+" ");
                }
            }

            System.out.println("");
            if (i==2 || i==5) System.out.println("----------------------");
        }
    }
}

With the file Sudoku.txt 与文件Sudoku.txt

.1.3..8..
5.96..7..
7.4.95.2.
4.....1..
.28.71.63
...2.495.
6.3..9..7
...43.516
.52.8..4.

Outputs the following: 输出以下内容:

. 1 . | 3 . . | 8 . . 
5 . 9 | 6 . . | 7 . . 
7 . 4 | . 9 5 | . 2 . 
----------------------
4 . . | . . . | 1 . . 
. 2 8 | . 7 1 | . 6 3 
. . . | 2 . 4 | 9 5 . 
----------------------
6 . 3 | . . 9 | . . 7 
. . . | 4 3 . | 5 1 6 
. 5 2 | . 8 . | . 4 . 

Not sure but I think using input.next() as a while statements results in loosing this value later on ( when you are using input.nextInt() it' s next value from a file, not the one from the statement). 不确定,但我认为将input.next()用作while语句会导致以后失去该值(当您使用input.nextInt()它是文件中的下一个值,而不是语句中的下一个值)。 Try using integer as a temporary container for that value: 尝试使用整数作为该值的临时容器:

int tmp;
while(tmp=input.next()!=null) and later on a[i][j]= tmp;

or you can try using different statement like while( input.hasNext() ) 或者您可以尝试使用其他语句,例如while( input.hasNext()

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

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