为什么我的 DAO 读取 .txt 文件而不存储所有数据行?

Why does my DAO read a .txt file and not store all lines of data?

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

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

注意:这是我第一次使用 Whosebug,所以如果有任何遗漏请告诉我。

代码:

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"

您的代码中有几个问题。

1) 您在条件检查中落后一分

j < (i + noofyeardata) 必须是 j <= (i + noofyeardata)

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

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

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>
....