我正在尝试读取一个大文本文件并使用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]);
}
}
非常感谢。