我有一个CSV文件,我需要在Scanner
的帮助下逐行阅读,并且只将国家/地区名称存储到一个字符串数组中。这是我的CSV文件:
World Development Indicators
Number of countries,4
Country Name,2005,2006,2007
Bangladesh,6.28776238,13.20573922,23.46762823
"Bahamas,The",69.21279415,75.37855087,109.340767
Brazil,46.31418452,53.11025849,63.67475185
Germany,94.55486999,102.2828888,115.1403608
这是我到目前为止所做的:
public String[] getCountryNames() throws IOException, FileNotFoundException{
String[] countryNames = new String[3];
int index = 0;
BufferedReader br = new BufferedReader(new FileReader(fileName));
br.readLine();
br.readLine();
br.readLine();
String line = br.readLine();
while((br.readLine() != null) && !line.isEmpty()){
String[] countries = line.split(",");
countryNames[index] = countries[0];
index++;
line = br.readLine();
}
System.out.println(Arrays.toString(countryNames));
return countryNames;
}
输出:
[Bangladesh, Brazil, null]
出于某种原因,它会跳过巴哈马,#34;并且无法读德国。请帮助我,我已经坚持这种方法已经好几个小时了。感谢您的时间和精力。返回应该是一个字符串数组(国家/地区名称)。
答案 0 :(得分:1)
您似乎正在阅读太多行,如下所示:
String line = br.readLine(); // Reads 1 line
while((br.readLine() != null) && !line.isEmpty()){ // Reads 1 line per iteration (and doesn't store it in a variable)
String[] countries = line.split(",");
countryNames[index] = countries[0];
index++;
line = br.readLine(); // Reads another line per iteration
}
while
循环的正确语法是:
String line;
while((line = br.readLine()) != null && !line.isEmpty() && index < countryNames.length) {
String[] countries = line.split(",");
countryNames[index++] = countries[0];
}
注意如何在条件内而不是在循环体内分配line
。
答案 1 :(得分:0)
您的代码有两个问题需要解析此CSV文件。正如一些人所指出的那样,你在读者上多次调用readLine
并丢弃输出。每次从流中读取时,都会失去对当前读取点之前的任何数据的访问权限。因此,reader.readLine() != null
例如从流中读取新数据,检查它是否为空,然后立即将其删除,因为您还没有将其存储在变量中。这是您在阅读时丢失数据的主要原因。
第二个问题是你的分裂情况。您可以在逗号上拆分,这是有道理的,因为这是一个CSV文件,但您的数据也包含逗号(例如,&#34;巴哈马,&#34;)。您需要更具体的拆分条件,如this post中所述。
以下是一个示例(使用countryNames
而不是数组的列表,因为它更容易使用):
private static final String csv = "World Development Indicators\n"
+ "Number of countries,4\n"
+ "Country Name,2005,2006,2007\n"
+ "Bangladesh,6.28776238,13.20573922,23.46762823\n"
+ "\"Bahamas,The\",69.21279415,75.37855087,109.340767\n"
+ "Brazil,46.31418452,53.11025849,63.67475185\n"
+ "Germany,94.55486999,102.2828888,115.1403608\n";
public static String[] getCountryNames() throws Exception {
List<String> countryNames = new ArrayList<>();
//BufferedReader br = new BufferedReader(new FileReader(fileName));
BufferedReader br = new BufferedReader(new StringReader(csv));
br.readLine();
br.readLine();
br.readLine();
String line = br.readLine();
while (line != null && !line.isEmpty()) {
String[] countries = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
countryNames.add(countries[0]);
line = br.readLine();
}
System.out.println(countryNames);
return countryNames.toArray(new String[0]);
}