Java txt文件到数组(无列表)

Java txt文件到数组(无列表),java,arrays,java-io,Java,Arrays,Java Io,我试图读入一个包含81个整数的文件,并用这81个整数填充一个数组,因此我检查数字的有效性 public static int[][] ReadIn () { Scanner input = new Scanner(System.in); System.out.println("Please enter a filename: "); int[][] grid = new int[9][9]; for(int i = 0; i &

我试图读入一个包含81个整数的文件,并用这81个整数填充一个数组,因此我检查数字的有效性

public static int[][] ReadIn () {

        Scanner input = new Scanner(System.in);

        System.out.println("Please enter a filename: ");
        int[][] grid = new int[9][9];
        for(int i = 0; i < 9; i++)
            for(int j = 0; j < 9; j++)
                grid[i][j] = input.nextInt();


        File file = new File(input);

        BufferedReader br = new BufferedReader(new FileReader(file));

        String st;
        while((st = br.readLine()) != null)
            System.out.println(st);

        return grid;
    }
我不知道为什么会发生这个错误,也不知道如何解决它

publicstaticint[]readASolution(字符串文件名){
public static int[][] readASolution(String filename) {
        int[][] grid = new int[9][9];

        try {
            Scanner sc = new Scanner(new File(filename));
            int i = 0;

            while(sc.hasNext()) {
                int j = 0;
                String line = sc.nextLine();

                for( int x = 0; x < line.length(); x++) 
                    if( Character.isDigit(line.charAt(x)) ) {
                        grid[i][j] = Character.getNumericValue(line.charAt(x)); 
                        j++;
                    }
                if (i == 9) break;
                i++; 
            }
        } catch(FileNotFoundException e ) { 
            System.err.println( "Error: " + e );
        }
        return grid;
    }
int[][]网格=新int[9][9]; 试一试{ Scanner sc=新扫描仪(新文件(文件名)); int i=0; while(sc.hasNext()){ int j=0; 字符串行=sc.nextLine(); 对于(int x=0;x

这是我为处理这类问题的任何其他人提出的正确解决方案

好的,那就不要名单了。您需要从
扫描仪
获取字符串,而不是使用
扫描仪
本身,例如
新文件(input.next())
。请尝试
Scanner.nextLine()
public static int[][] readASolution(String filename) {
        int[][] grid = new int[9][9];

        try {
            Scanner sc = new Scanner(new File(filename));
            int i = 0;

            while(sc.hasNext()) {
                int j = 0;
                String line = sc.nextLine();

                for( int x = 0; x < line.length(); x++) 
                    if( Character.isDigit(line.charAt(x)) ) {
                        grid[i][j] = Character.getNumericValue(line.charAt(x)); 
                        j++;
                    }
                if (i == 9) break;
                i++; 
            }
        } catch(FileNotFoundException e ) { 
            System.err.println( "Error: " + e );
        }
        return grid;
    }