2

我一直在尝试从 .txt 文件中保存一个 9x9 2D 整数数组。我已经在这个网站上待了很长时间,试图让它工作,经过大量的调整,我非常接近它。唯一的问题是没有任何东西保存到数组中!

这是我的代码:

import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;


public class test {

public static void main(String[] args) throws IOException {


    int[][] thing = new int[9][9];
    int row = 0;
    int rows = 9;
    int columns = 9;


    File fin = new File("C:\\Users\\David\\workspace\\tester\\initial.txt");

    BufferedReader reader = null;

    try {
        reader = new BufferedReader(new FileReader(fin));
        String line = reader.readLine();
        int lineNum = 0;

        while (line != null) {
            lineNum++;
            System.out.println("line " + lineNum + " = " + line);
            line = reader.readLine();

            String [] tokens = line.split(",");
            for (int j=0; j<tokens.length; j++) {
                System.out.println("I am filling the row: " + row);
                thing[row][j] = Integer.parseInt(tokens[j]);
            }
            row++;
        }

        System.out.println("I am printing the array for testing purposes: ");
        for (int i=0; i < rows; i++) {
            for (int j = 0; j < columns; j++)
                System.out.print(thing[i][j]);
            System.out.println("");
        }
    } catch (IOException error) {

    } finally {
        if (reader != null) reader.close();
    }

}

}

我应该说我这样做是为了测试我正在尝试创建的数独游戏作为一个纯粹的副项目,我只是非常沮丧。

这也是我在这个网站上的第一篇文章,所以请放心格式化。谢谢大家!

编辑:我也做了 codaddict 告诉我的改变,现在我得到了输出:

line 1 = 5 3 0 0 7 0 0 0 0
I am filling the row: 0
Exception in thread "main" java.lang.NumberFormatException: For input string: "6 0 0 1 9 5 0 0 0"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at test.main(test.java:36)
4

3 回答 3

3

你正在做:

    while (line != null) {
        lineNum++;
        System.out.println("line " + lineNum + " = " + line);
        line = reader.readLine();
    }

    // At this point you've already reached the end of the file
    // and line is null, so you never go inside the next while.

    while (line != null) {
        // you need to split on space not comma
        String [] tokens = line.split(" ");
        for (int j=0; j<tokens.length; j++) {
            System.out.println("I am filling the row: " + row);
            thing[row][j] = Integer.parseInt(tokens[j]);
        }
        row++;
    }

要解决此问题,您需要处理外部 while 循环中的每一行:

    while (line != null) {
        lineNum++;
        System.out.println("line " + lineNum + " = " + line);
        line = reader.readLine();

        String [] tokens = line.split(",");
        for (int j=0; j<tokens.length; j++) {
            System.out.println("I am filling the row: " + row);
            thing[row][j] = Integer.parseInt(tokens[j]);
        }
        row++;
    }
于 2012-12-14T12:05:11.193 回答
1

退出第一个 while 循环后,该字符串line已经为空,因此第二个 while 循环永远不会执行,因此不会为数组单元分配任何值,int 数组单元的默认值为零,它会被打印出来。在第一个循环中插入分配部分以解决问题。

于 2012-12-14T12:04:59.023 回答
0

您已经在第一个 while 循环中读取了整个文件。

因此,您无法打印第二个循环中的值。

确保将文本文件中的值存储在第一个 while 循环中。

while (line != null) {
    lineNum++;
    System.out.println("line " + lineNum + " = " + line);
    line = reader.readLine();

    String [] tokens = line.split(",");
    for (int j=0; j<tokens.length; j++) {
        System.out.println("I am filling the row: " + row);
        thing[row][j] = Integer.parseInt(tokens[j]);
    }
    row++;
}
于 2012-12-14T12:06:13.283 回答