[英]Why isn't my object storing into an array each time I read a line of a file? Java
I'm reading data in from a text file. 我正在从一个文本文件中读取数据。 Each line has a year at the beginning of the line followed by some data for each month. 每行的开头都有一年,然后是每个月的一些数据。 I'm trying to save both the year and month data into an object and storing it into an object array for each line I read. 我试图将年和月的数据都保存到一个对象中,并将其存储到每个读取的行的对象数组中。 I'm pretty sure I've done everything right apart from my parseFile method. 我很确定除了parseFile方法之外,我已经完成了所有操作。 Here it is - 这里是 -
while ((line1 = word_reader.readLine()) != null) {
int year;
double[] monthlyRain = new double[12];
String[] values1 = line1.split(",");
// validation
if (values1.length == 13){
year = Integer.parseInt(values1[0]);
for (int i = 1; i < values1.length; i++) { // Start from 1.
monthlyRain[i - 1] = Double.parseDouble(values1[i]);
}
int i =0;
rainfallYears[i] = new RainfallYear(year,monthlyRain);
i++;
}
}
I thought the lines towards the bottom after the for loop would work but it isn't. 我认为for循环后朝底部的行会起作用,但事实并非如此。
you place your object always at index 0: 您将对象始终放置在索引0处:
int i =0;
rainfallYears[i] = new RainfallYear(year,monthlyRain);
put i
outside the while-loop.
把i
放在while-loop.
: :
int i = 0;
while ((line1 = word_reader.readLine()) != null) {
then it should work. 那么它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.