I am having difficulties reading string number values from a .csv file and converting them into double values. Here is the code:
String filename = "/home/mahmoud/Desktop/CSV/remake1.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
int val;
try {
br = new BufferedReader(new FileReader(filename));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] country = line.split(cvsSplitBy);
val = Integer.parseInt(country[4]);
System.out.println(val);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done");
}
When I compile the program I get the following error "Exception in thread "main" java.lang.NumberFormatException: For input string: "6.55"
Here is the content of the .csv file:
2012-12-03,5.63,6.57,5.32,6.55,5816300,6.55
2012-11-01,4.66,5.68,4.37,5.59,5752000,5.59
2012-10-01,5.27,5.40,4.42,4.67,5958000,4.67
2012-09-04,5.91,6.56,5.21,5.26,5930600,5.26
any suggestion on how to fix that problem? Thank You
float, why are you usingInteger.parseInt()? Declarefloat val;, and useFloat.parseFloat()instead.Double.parseDouble()floatwould be sufficient.