我对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"

最佳答案

您的代码中有几个问题。

1)在条件检查中您一人离开

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

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

还有一个一般点1。完成内部循环后,无需从上次中断的地方继续进行外部循环(i计数器)。您可以在for循环体内添加inoofyeardatai = 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>
....

关于java - 为什么我的DAO读取.txt文件而不存储所有数据行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53484554/

10-11 20:50