Home > Back-end >  How can I remove punctuaton and letters from CSV?
How can I remove punctuaton and letters from CSV?

Time:03-01

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.

CodePudding user response:

There might be other ways but this is what i think could be easier for you.

String[] fields = line.split(";");
// removing 1st char from 1st word
fields[0] = fields[0].substring(1)

// removing currency from last field
// split by space and get the first word
fields[fields.length-1] = fields[fields.length-1].split(" ")[0];
//

more ways to split by space: How to split a String by space

CodePudding user response:

The problem are the double quotes around all. Do first:

line = line.replaceAll("^\\s*\"([^\"]*)\"\\s*$", "$1")
  • Related