2

.txt ファイルから 9x9 の 2D 配列の int を保存しようとしています。私はこれを機能させるために非常に長い間このWebサイトにアクセスしてきました. 唯一の問題は、配列に何も保存されないことです!

これが私のコードです:

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はすでにnullであり、したがって2番目のwhileループは実行されないため、配列セルに値は割り当てられません。int配列セルのデフォルト値はゼロであり、出力されます。問題を解決するために、最初のループに割り当て部分を挿入します。

于 2012-12-14T12:04:59.023 に答える
0

最初の while ループ自体でファイル全体を読みました。

したがって、2 番目のループで値を出力することはできません。

最初の 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 に答える