简体   繁体   English

为什么每次读取文件行时我的对象都没有存储到数组中? 爪哇

[英]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.

相关问题 如何读取一行中每个int的.txt文件并存储到数组中? - How to read a .txt file for each int on a line & storing to array? 为什么Java找不到我的文件 - Why isn't java finding my file 在Java中,我需要读取一个文本文件并将每一行放在单独的数组中。 但是每次阅读文本文件时,我都无法分开 - In Java I need to read a text file and put each line in a separate array. But every time I read the text file I cannot split the lines 为什么我的PHP文件无法读取/接收我的Java输出流 - Why isn't my outputstream in Java being read/received by my PHP file 为什么我的对象没有被添加到我的数组中? - Why isn't my object being added to my array? 为什么命令行没有找到我的.java文件? - Why isn't command line not finding finding my .java files? 我不知道为什么我的数组没有填写 Java - I can't figure out why my array isn't filling here in Java 一次读取一行 CSV 文件,然后在循环内将每一行解析为 Class 字段,然后将该 Class Z4970317944151AC3524B 存储到数组中 - Read a CSV file one line at a time and then within the loop parse each line into the Class fields and then store that Class Object into an array 为什么我的Java LinkedHashSet不删除它包含的对象? - Why isn't my Java LinkedHashSet removing an object it contains? 为什么没有 java.lang.Array 类? 如果一个java数组是一个对象,它不应该扩展对象吗? - Why isn't there a java.lang.Array class? If a java array is an Object, shouldn't it extend Object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM