我正在尝试读取一个大文本文件并使用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]);
}
}
非常感谢。
答案 0 :(得分:2)
首先创建实例
public String[][][] sortedData = new String[n][n2][n3]; //n1 n2 n3 dimension size
答案 1 :(得分:1)
你忘了这个:
sortedData = new String[34][12][31];