2

In my system I have an Excel reader that also reads csv files.

This is the way I read the csv file:

        Path path = Paths.get(this.getFilePath());
        CSVParser parser = new CSVParserBuilder().withSeparator(';').build();

        try(BufferedReader br = Files.newBufferedReader(path, StandardCharsets.UTF_8);
            CSVReader reader = new CSVReaderBuilder(br).withCSVParser(parser).build()) {

            rows = reader.readAll();

This works. The data from the csv file is stored in a list of string arrays.

Now I noticed something when reading the data from the list which I don't understand.

This is the way I read the data from the list:

if (rows != null && rows.size() > 1) {
            for (String[] row : rows) {
                                    
                int i = Integer.parseInt(row[0]);
                String name = row[1];
                double d = Double.parseDouble(row[2].replace(",", "."));

            }
        }

If the first array is fetched from the list and the first string from the array is parsed from String to int, java.lang.NumberFormatException: For input string: " 27" is thrown.

But if the first array (first excel line) is skipped:

            if (rows != null && rows.size() > 1) {
            for (int i = 1; i < rows.size(); i++) {  //i = 1, skipp first array(excel line)
                String[] row = rows.get(i);

                int i = Integer.parseInt(row[0]);
                String name = row[1];
                double d = Double.parseDouble(row[2].replace(",", "."));

            }
        }

This way no java.lang.NumberFormatException: For input string: is thrown.

I do not understand why a java.lang.NumberFormatException is thrown if the first array (excel line) is not skipped.

The first line from the excel file is needed and should not be skipped. Can it have something to do with reading the excel file? With the reader I use?

Does anyone know this problem or can anyone help me?

Hubi
  • 440
  • 1
  • 11
  • 25
  • That is the thing: reading CSV is much more complicated than splitting on ",". Seriously: there is a reason why there are libraries for that - it isnt trivial. Only do "manual" CSV parsing when you are 100% sure that your input files will always look the same. Otherwise, you will run from one bug into the next one, repeating all the mistakes that one can make when re-implementing CSV parsing for the Nth time. – GhostCat Aug 21 '20 at 13:16
  • And note: your question title doesnt match the question body at all. Focus your content: do you want to know how you could be parsing CSV with the first line, or do you want to know why YOUR parsing code fails?! – GhostCat Aug 21 '20 at 13:18
  • @GhostCat right...changed the title. – Hubi Aug 21 '20 at 13:48

3 Answers3

2

You get these error, because your number has a leading blank. You can use trim before parsing the number. These should remove the blank(s) and the parse error.

        if (rows != null && rows.size() > 1) {
        for (int i = 1; i < rows.size(); i++) {  //i = 1, skipp first array(excel line)
            String[] row = rows.get(i);

            int i = Integer.parseInt(row[0].trim());
            String name = row[1];
            double d = Double.parseDouble(row[2].replace(",", "."));

        }
    }

You can also try:

Integer.parseInt(row[0].replaceAll("\\D+", ""));

This removes allnon Digits from the String.

Jens
  • 67,715
  • 15
  • 98
  • 113
  • Even if i trim the String the exception is thrown... java.lang.NumberFormatException: For input string: "27" – Hubi Aug 21 '20 at 13:17
  • @Hubi iit can not be, because 27 is a valid number. The only possibility that i see is, that thre are non printable characters in front or after the number 27. Can you check it please? – Jens Aug 21 '20 at 13:20
  • Yeah thank you...this works, i find it a little hard to understand it, even when i look at the file i do not see any spaces or non printable characters... Thanks! – Hubi Aug 21 '20 at 13:31
  • 1
    If this is cell 0 of row 0, maybe it contains a [byte-order mark](https://en.wikipedia.org/wiki/Byte_order_mark). When you "looked" at the file, what did you look at it with? You need something that will show raw bytes, maybe a hex editor. – user13784117 Aug 21 '20 at 15:00
1

The string it's trying to parse has a leading space: " 27". That is what's causing the error. The following code also throws the exception:

public class MyClass {
    public static void main(String args[]) {
      int i = Integer.parseInt(" 27");
      System.out.println(i);
    }
}

As a general rule you should sanitize your input before parsing it. For example, if it's going to be a numeric value, trim() the string before feeding it to parseInt().

JustAnotherDeveloper
  • 2,061
  • 2
  • 10
  • 24
1

Your parsing is not adequate. The error you get shows that one cell contains a number with a leading space.

java.lang.NumberFormatException: For input string: " 27"

You call Integer.parseInt() on this cell value. By its documentation, parseInt() rejects leading spaces.

You will need to trim the value before calling parseInt. See String.trim().

user13784117
  • 1,124
  • 4
  • 4
  • Even if i trim the String the exception is thrown... java.lang.NumberFormatException: For input string: "27" – Hubi Aug 21 '20 at 13:17
  • I accepted the second suggestion of this answer, it solved my problem – Hubi Aug 21 '20 at 13:35