首页 > 解决方案 > 为什么我的 DAO 读取 .txt 文件而不存储所有数据行?

问题描述

我对 Java 相当陌生,目前正在开发一个小型应用程序,该应用程序可以存储和操作从文本文件中读取的城市数据。我有一个 DAOTextImpl 类,它应该从文本文件中读取,选择所有数据行并将它们存储在要排序的 ArrayList 中。到目前为止,我只能获取它要存储的 4 行数据中的 3 行。

有人可以看看为什么只有前两组城市数据被提取和存储(2015 年和 2016 年)?

注意:这是我第一次使用 StackOverflow,所以如果我遗漏了什么,请告诉我。

代码:

public class DaoTextImpl implements DAOInterface {

static final char DELIMITER=',';

@Override
public Repository load(String filename) {

    Repository repository = new Repository();

    try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
        String[] temp;
        String line;
        ArrayList<String> lines = new ArrayList<>();
        ArrayList<YearData> yearData = new ArrayList<>();
        while ((line = br.readLine()) != null){
            lines.add(line);
        }
        for (int i = 0; i < lines.size(); i++) {
            temp = lines.get(i).split(Character.toString(DELIMITER));
            if (temp.length == 4) {
                int id = Integer.valueOf(temp[0]);
                String cityName = stripQuotes(temp[1]);
                String country = stripQuotes(temp[2]);
                int noofyeardata = Integer.valueOf(3);

                for (int j = (i + 1); j < (i + noofyeardata); j++) {
                    String[] yearDataArray = lines.get(j).split(Character.toString(DELIMITER));
                    String year = stripQuotes(yearDataArray[0]);
                    float precipitation = Float.valueOf(yearDataArray[1]);
                    int maxtemp = Integer.valueOf(yearDataArray[2]);
                    int mintemp = Integer.valueOf(yearDataArray[3]);
                    int windspeed = Integer.valueOf(yearDataArray[4]);
                    String winddirection = stripQuotes(yearDataArray[5]);
                    yearData.add(new YearData(year, precipitation, maxtemp, mintemp, windspeed, winddirection));
                }
                repository.add(new City(id, cityName, country, yearData));
            }
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return repository;
}

文本文件内容:

1,"Cartagena","Spain",3
"2015",0.2,33,26,6,"S"
"2016",0.0,33,24,8,"SSW"
"2017",0.0,32,25,6,"E"
2,"Glasgow","Scotland",3
"2015",0.0,19,8,3,"SE"
"2016",0.1,21,11,6,"SE"
"2017",2.1,19,11,9,"SW"
3,"Valencia","Spain",4
"2015",0.0,34,24,6,"SE"
"2016",0.0,39,23,5,"SSE"
"2017",0.0,32,24,5,"E"
"2014",0.0,29,20,6,"ESE"

标签: javaarraylistdao

解决方案


您的代码中有几个问题。

1) 你在状态检查中落后一个

j < (i + noofyeardata)一定是j <= (i + noofyeardata)

2)int noofyeardata = Integer.valueOf(3)必须是int noofyeardata = Integer.valueOf(temp[3])。这就是为什么你总是读noofyeardata3

更一般的一点1。完成内循环后,您无需i从中断处继续外循环(计数器)。您可以在fori循环体内增加noofyeardata( i = i + noofyeardata + 1) (因为您已经处理了行)。有了这个,您还可以删除if (temp.length == 4)检查。noofyeardata

for (int i = 0; i < lines.size();) {
        temp = lines.get(i).split(Character.toString(DELIMITER));
        int id = Integer.valueOf(temp[0]);
        String cityName = stripQuotes(temp[1]);
        String country = stripQuotes(temp[2]);
        int noofyeardata = Integer.valueOf(temp[3]);

        for (int j = (i + 1); j <= (i + noofyeardata); j++) {
           //your existing code
        }
        repository.add(new City(id, cityName, country, yearData));
        i += noofyeardata + 1;
    }
}

1这是假设文件的内容严格遵循您提到的数据格式。

<id>, <cityName>, <country>, <noofyeardata1>
<noofyeardata1 rows>
<id>, <cityName>, <country>, <noofyeardata2>
<noofyeardata2 rows>
....

推荐阅读