0

私は本当にあなたの助けが必要です。TXT ファイルの文字を多次元配列に入れたいのですが、Java コードがうまくいきません。Scanner.util を使用してファイルを読み取る必要があります。これが私のコードです

import java.io.*;
import java.util.Scanner;


public class Test {
public static void main(String args[]) {
try {



        FileInputStream file = new FileInputStream("a.txt");
        Scanner s = new Scanner(file);
        char arr[][];

        while (s.hasNextLine()) {
             String line = s.nextLine();
            for (int i=1 ;i<=line.length(); i++){
                for (int k=1 ;k<=line.length(); k++){
                arr[k][i] = line.charAt(i);          //the local variable arr may not have been initialized

                }
            }
            for (int i=0 ;i<=line.length(); i++){
                int k=0;
                System.out.println (arr[i][k]);      //the local variable arr may not have been initialized
                k ++;
            }
        }


        file.close();
    } catch (IOException e) {
        System.out.println("Fehler");
    }
}
}

コードを修正するのを手伝ってもらえますか? それは本当にいいでしょう:)


編集:

わかった。これは今のように見えます:

public class Test {
public static void main(String args[]) {
try {



    FileInputStream file = new FileInputStream("a.txt");
    Scanner s = new Scanner(file);
    char arr[][] = new char[15][9]; //changed

    while (s.hasNextLine()) {
         String line = s.nextLine();
        for (int i=0 ;i<=line.length(); i++){ //changed
            for (int k=0 ;k<=line.length(); k++){ //changed
            arr[k][i] = line.charAt(i);

            }
        }
        for (int i=0 ;i<=line.length(); i++){
            int k=0;
            System.out.println (arr[i][k]);
            k ++;
        }
    }


    file.close();
} catch (IOException e) {
    System.out.println("Fehler");
}
}
}

これは私が読みたかったTXTファイルです:

---------------
|S    |  |    |
| --- | ----  |
|  |  |     | |
|  |  |---- |Z|
|   | |     | |
| |   |  |  | |
| | |    |    |
---------------

しかし、プログラムを開始すると、次のエラーが発生します。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
at maze.Test.main(Test.java:21)

私が間違っていることを知っていますか?-.-


わかった。これは今のように見えます:

public class Test {
public static void main(String args[]) {
try {



    FileInputStream file = new FileInputStream("a.txt");
    Scanner s = new Scanner(file);
    char arr[][] = new char[15][9]; //changed

    while (s.hasNextLine()) {
         String line = s.nextLine();
        for (int i=0 ;i<=line.length(); i++){ //changed
            for (int k=0 ;k<=line.length(); k++){ //changed
            arr[k][i] = line.charAt(i);

            }
        }
        for (int i=0 ;i<=line.length(); i++){
            int k=0;
            System.out.println (arr[i][k]);
            k ++;
        }
    }


    file.close();
} catch (IOException e) {
    System.out.println("Fehler");
}
}
}

これは私が読みたかったTXTファイルです:

---------------
|S    |  |    |
| --- | ----  |
|  |  |     | |
|  |  |---- |Z|
|   | |     | |
| |   |  |  | |
| | |    |    |
---------------

しかし、プログラムを開始すると、次のエラーが発生します。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
at maze.Test.main(Test.java:21)

私が間違っていることを知っていますか?-.-

4

1 に答える 1

0

配列を初期化する必要があります。

    char arr[][] = new char[100][100];

また、最初のループでは、変数 i と k は 0 から開始する必要があります。プラス <= を削除し、未満に変更します。

于 2013-01-26T03:58:24.740 に答える