0

大きなテキスト ファイルを読み取って、scanner() を使用して 3D 配列に並べ替えようとしていますが、配列にデータが入力されていないと思いますjava.lang.NullPointerException。それ。

現在、ネストされた for ループを使用して、年、月、および各日付を整理しようとしています。

私がやりたいもう1つのことは、配列をからStringに変更することですintが、それはありません。

これが私のコードです:

public class GetData {

    public String[][][] sortedData;

    private Scanner rainFile;

    //method for opening the file
    public void openFile() {

        try{
            rainFile = new Scanner(new File("myfile.txt"));
        }
        catch(Exception e){
            System.out.println("Could not find file");
        }
    }

    //method for reading the file
    public void readFile(){

        int month = 0;
        int day = 0;

        //this loop sorts each year
        for(int l = 0; l < 34; l++){
            String a = rainFile.next();
            sortedData[l][month][day] = a;

                //this loop sorts every month of each year
                for(int i = 0; i < 12; i++){
                    String b = rainFile.next();
                    sortedData[l][i][day] = b;
                    month++;

                        //this loop sorts each individual entry of every month
                        for(int j = 0; j < 31; j++){
                            String c = rainFile.next();
                            sortedData[l][i][j] = c;
                            day++;
                        }
                    }        
                }

        }

        //close the file once it's been used
        public void closeFile(){
            rainFile.close();
        }

        //test method to see if array is full
        public void arrayTest(){
            System.out.print(sortedData[1][1][1]);
        }
}

どうもありがとう。

4

2 に答える 2

2

最初にインスタンスを作成

public String[][][] sortedData = new String[n][n2][n3]; //n1 n2 n3 dimension size
于 2013-05-17T11:48:32.680 に答える
1

あなたはこれを忘れました:

sortedData = new String[34][12][31];
于 2013-05-17T11:50:22.767 に答える