I'm making a toto-appilcation where I have to read data from the CSV file. Unfortunately, my program does not work, the following error occurs:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""2015"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:654)
at java.base/java.lang.Integer.parseInt(Integer.java:786)
at com.epam.training.toto.data.FileBasedDataStore.init(FileBasedDataStore.java:31)
at com.epam.training.toto.App.main(App.java:20)
The CSV file consist of semicolon separated data, a leading punctuation mark, and a trailing one; just like this:
"2015;1;2;24;240 EUR"
(I have more data,I just wanted to show the example)
Basically I'm trying to get rid of the punctuation marks and the type of valute, so it won't make compiler errors when I'm trying to parse the money
filed into an int
.
I tried several methods I found on the internet, however it either throws error due to the "
or it does not remove the EUR
or it replaces all with " "
, which also throws an error.
The part of my code which should handle the parsing looks like this currently:
try (BufferedReader reader = new BufferedReader(new FileReader(init))) {
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.replaceAll("\\p{Punct}" + "EUR", " ").split(";");
int year = Integer.parseInt(fields[0]);
int week = Integer.parseInt(fields[1]);
etc...
Dont throw stones at me please, I'm fairly a beginner.