I have a CSV file that I need to read line by line with the help of a Scanner
and store only country names into an array of strings. Here is my CSV file:
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
This is what I have so far:
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;
}
Output:
[Bangladesh, Brazil, null]
For some reason it skips "Bahamas, The" and can't read Germany. Please help me, I have been stuck on this method for hours already. Thanks for your time and effort. The return should be an array of Strings (country names).