gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:33:12 25 4
gpt4 key购买 nike

我对 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]) 。这就是为什么您总是阅读 noofyeardata3

还有一点1。完成内部循环后,无需从中断处继续外部循环(i 计数器)。您可以增加 i通过noofyeardata ( i = i + noofyeardata + 1 ) 在 for 循环体内(因为您已经处理了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com