java - 如何将大文本文件排序为多维数组?

标签 java arrays sorting multidimensional-array java.util.scanner

我正在尝试读取一个大文本文件并使用scanner()将其排序到3D数组中,但我认为该数组没有被填充,并且我不断收到错误java.lang.NullPointerException ,但我怀疑问题还不止于此。

我目前正在尝试使用嵌套的 for 循环来排序年、月和每个日期。

我想做的另一件事是将数组从 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]);
        }
}

非常感谢。

最佳答案

先创建实例

public String[][][] sortedData = new String[n][n2][n3]; //n1 n2 n3 dimension size

关于java - 如何将大文本文件排序为多维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16608220/

相关文章:

java - Java 程序中方案 : hdfs, 没有文件系统

php - 映射数组并且不让 future 的开发人员搞砸的最佳方法是什么?

javascript - 使用 formData() 上传多个文件

java - 对两个整数的 ArrayList 进行排序

java - c++ "interface"-类似于Java的类?

java - 如何在 java 上设置日期的 24 小时格式?

Java:在跳过中间继承的父类(super class)时调用基父类(super class)方法

c# - 将整数数组随机化

php - 如何按日期对 Twitter 好友/关注者进行排序?

algorithm - 二分查找与简单查找