我有一个非常大的数据文件。它以一个我想忽略或从等式中删除的大段落开始,然后它有一个年份,例如 1974,然后是 6 个空格,然后是一个代表一个月的数字,例如 1,然后它有 31 个我想要的数据条目排序为 3D 数组。
我想我已经接近解决它了,但我不断收到异常java.lang.ArrayIndexOutOfBoundsException
。
完整的错误是:
线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 31 at pkg110_term3.GetData.readFile(GetData.java:48) at pkg110_term3.Main.main(Main.java:25) Java 结果:1
这是我的代码:
public class GetData {
public String[][][] sortedData = new String[34][12][31];
int a = 0;
int b = 0;
int c = 0;
private Scanner rainFile;
//method for opening the file
public void openFile() {
try{
rainFile = new Scanner(new File("C:\\\\Users\\\\admin\\\\Documents\\\\NetBeansProjects\\\\110_term3\\\\WeatherDataFiles\\\\rainfall.txt"));
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "Check the file path is correct.", "No file found!", JOptionPane.ERROR_MESSAGE);
}
}
//method for reading the file
public void readFile(){
if(rainFile.equals("Daily Rainfall Totals (mm), Hazelrigg Weather Station, Lancaster University (Meteorological Office Climatological Station Number 7236) \n" +
"Brian Davison, LEC, Lancaster University, data obtained March 2012 \n" +
"Format=Year, Month, 1-31 daily precipitation values. Any entry set to -99.99 means that no data exists for that day. ")){
rainFile.remove();
}
while(rainFile.hasNext()){
rainFile.next();
a++;
while(rainFile.hasNext()){
rainFile.next();
b++;
for(int i = 0; i < 31; i++){
String day = rainFile.next();
sortedData[a][b][c] = day;
c++;
}
}
}
}
//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[0][0][0]);
System.out.print("\n");
System.out.print(sortedData[0][1][0]);
System.out.print("\n");
}
}
我最近确实问了一个类似的问题,但从那以后我的代码发生了很大变化。
提前致谢。
前2年的降雨数据+我要去掉的那段:
Daily Rainfall Totals (mm), Hazelrigg Weather Station, Lancaster University (Meteorological Office Climatological Station Number 7236)
Brian Davison, LEC, Lancaster University, data obtained March 2012
Format=Year, Month, 1-31 daily precipitation values. Any entry set to -99.99 means that no data exists for that day.
1974 1 0.00 0.00 1.70 3.30 8.70 2.30 12.70 13.10 0.01 13.50 1.50 5.50 1.90 15.40 6.50 7.70 4.00 0.10 0.00 0.00 0.00 8.00 0.20 0.01 0.01 7.00 8.00 0.20 2.30 7.00 0.20
1974 2 6.60 14.90 0.70 5.00 7.30 0.80 0.20 9.40 5.50 10.50 5.50 2.50 0.80 6.90 2.50 0.30 0.00 0.01 0.00 1.80 0.40 0.00 0.00 0.20 0.01 0.01 0.01 7.40 -99.99 -99.99 -99.99
1974 3 2.20 0.00 0.60 0.60 0.00 12.80 0.01 0.01 0.01 0.10 0.01 0.00 0.50 3.70 6.80 2.50 9.10 1.50 0.01 0.01 0.00 0.00 0.00 0.20 0.20 0.00 0.00 0.00 0.00 0.00 0.00
1974 4 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.70 0.80 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.00 0.01 0.00 0.01 0.00 0.00 -99.99
1974 5 35.60 0.90 0.60 0.00 0.01 0.00 0.00 0.10 4.50 2.40 0.01 2.70 3.00 0.00 0.00 0.01 0.01 1.20 1.30 3.10 1.00 7.40 7.30 0.01 0.01 0.01 1.00 4.10 0.00 0.00 0.01
1974 6 1.80 0.01 0.00 0.01 15.50 8.80 3.90 3.50 2.20 5.90 0.00 0.00 0.00 0.00 5.20 4.00 0.01 6.20 0.01 0.00 0.00 0.00 0.00 0.00 0.00 0.01 0.00 0.00 0.00 16.70 -99.99
1974 7 0.01 32.30 3.70 16.40 0.01 1.70 0.10 0.20 2.90 1.80 0.01 0.00 0.10 11.50 16.00 2.20 0.01 0.80 5.20 0.00 0.01 10.00 0.10 0.00 6.80 0.00 4.00 1.30 15.00 9.20 4.20
1974 8 0.00 0.00 0.00 0.00 0.00 0.00 0.10 7.30 19.40 4.40 16.40 0.01 0.10 5.60 0.00 1.20 0.00 0.00 0.00 0.00 1.00 1.70 0.10 0.20 3.80 7.60 0.01 0.00 0.01 0.00 5.50
1974 9 1.40 7.60 3.90 29.80 12.90 12.80 16.80 22.40 1.10 0.01 0.50 12.00 0.01 0.50 0.20 8.50 0.01 0.00 1.20 8.80 1.40 12.70 7.90 19.80 2.20 0.70 16.40 0.00 0.01 0.01 -99.99
1974 10 12.30 0.20 6.20 0.01 0.01 11.30 0.20 0.00 0.00 0.10 0.00 0.00 0.00 0.01 5.10 0.01 4.20 16.80 8.30 0.50 0.01 0.01 0.00 0.70 0.20 2.70 1.10 0.00 0.00 4.30 2.40
1974 11 0.01 8.40 0.80 0.00 0.90 0.20 9.10 2.60 6.00 11.60 6.00 2.30 15.60 6.40 3.20 0.01 0.00 0.00 2.50 0.01 0.30 1.60 5.60 11.50 2.30 0.90 3.40 0.01 8.10 2.10 -99.99
1974 12 0.80 0.30 2.30 0.90 0.01 0.60 7.80 1.20 5.90 8.70 4.80 1.20 4.00 0.00 7.80 5.20 5.20 1.80 0.00 4.30 2.30 0.01 7.80 2.70 10.00 2.00 3.80 1.60 0.50 1.50 0.00
1975 1 3.30 1.40 0.01 2.20 0.10 16.80 4.70 0.40 0.10 5.70 1.60 7.60 1.80 4.00 2.10 10.70 7.20 1.00 6.80 4.30 25.10 10.20 0.20 13.20 3.30 6.70 2.10 0.70 2.10 8.20 5.90
1975 2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 3.10 0.80 1.60 6.80 1.70 0.00 16.00 0.20 0.00 0.00 0.90 0.00 0.00 2.50 0.00 0.00 0.00 0.00 0.00 -99.99 -99.99 -99.99
1975 3 0.50 2.20 0.50 9.90 0.00 6.90 0.00 0.00 0.01 0.10 0.10 0.00 0.00 0.01 0.00 0.01 0.01 0.01 0.00 0.00 15.60 0.01 0.00 0.01 8.80 1.10 0.01 0.00 0.20 1.00 0.00
1975 4 2.80 0.01 0.01 0.01 0.01 3.10 0.01 0.01 0.40 4.40 2.40 1.20 2.00 7.80 0.10 5.40 3.60 6.20 1.80 7.60 2.80 0.10 0.10 0.00 0.00 0.00 0.00 5.40 0.30 8.00 -99.99
1975 5 2.10 0.01 0.00 0.00 0.00 0.00 1.20 4.80 1.80 0.80 0.10 2.10 5.20 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.01 0.00 0.01 0.01 0.00 0.00 0.00 0.00 0.20 0.00
1975 6 3.10 1.90 3.40 5.60 4.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.10 0.00 0.40 0.00 0.01 1.10 1.50 1.40 0.00 0.00 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.00 -99.99
1975 7 0.00 0.00 0.01 0.01 0.00 0.00 0.01 0.10 7.20 7.90 0.01 5.50 5.90 18.10 0.01 0.00 7.00 0.01 4.10 2.20 15.60 32.80 2.50 7.20 2.50 0.01 0.00 0.01 0.01 0.01 0.00
1975 8 0.00 0.00 0.00 2.80 2.70 0.00 1.30 5.80 3.60 0.00 0.00 0.01 0.00 11.40 26.70 0.30 0.00 0.01 5.20 0.20 8.00 0.00 0.80 4.60 0.00 0.00 0.00 0.00 7.50 0.50 0.00
1975 9 0.00 0.40 0.01 5.40 2.00 0.90 1.30 9.70 13.70 23.50 18.90 0.20 0.00 0.00 0.00 0.00 6.90 0.30 3.80 1.80 0.00 7.40 0.60 32.00 5.60 3.40 13.50 0.00 4.60 19.90 -99.99
1975 10 8.30 21.10 0.20 5.00 0.20 0.00 0.00 0.01 1.80 0.00 0.00 0.00 0.01 0.10 1.20 1.20 0.00 0.00 0.00 0.00 0.00 0.01 6.00 0.00 0.01 0.00 0.00 0.00 0.01 0.40 3.20
1975 11 8.20 9.70 0.01 8.70 4.90 0.01 0.00 0.00 0.50 0.40 0.10 0.00 0.00 5.80 17.50 0.01 0.00 4.60 3.00 0.00 0.00 0.20 5.00 4.20 11.50 6.80 7.20 0.01 3.10 5.00 -99.99
1975 12 43.20 0.01 0.01 0.01 0.01 0.40 0.01 0.01 0.01 0.01 3.20 0.00 0.01 0.00 0.01 0.20 0.00 1.30 0.20 0.01 0.01 0.01 3.10 8.40 0.01 0.01 0.80 0.10 0.40 3.20 10.70
我刚刚意识到每个月都用一条线隔开,我可以用它来简化排序吗?