我正在尝试读取一个大文本文件并使用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/