Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java CSV file parsing does not parse empty columns at end

Tags:

java

csv

I am parsing a CSV file, however there is one row were the last 9 columns are empty and the string split by comma ignores the remains empty columns.

Here is code to demonstrate this:

String s="L2,,,,,,,,,,,,,,,,,,108.50,-188.04,,,,,,,,,";
String[] columns = s.split(",");
System.out.println(columns.length);

The size of columns is 20, when it should be 29. Any ideas??

like image 549
Andy Avatar asked Apr 03 '15 22:04

Andy


2 Answers

Look at the documentation for String.split:

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

So you need to look at the options for the other split method

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

Emphasis mine.

You need:

String[] columns = s.split(",", -1);
like image 65
Boris the Spider Avatar answered Nov 13 '22 01:11

Boris the Spider


The docs of split says:

If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

split(String regex) calls the overloaded split method with a limit of 0.That's why you have an array length of 20, all the empty strings after -188.04 are discarded in the resulting array.

If you want to get all the empty trailing strings, you can give a negative limit to say that you want to apply the pattern as many times as possible.

String[] columns = s.split(",", -1); //length is 29 there

Although it's not hard to parse CSV, you may also want to look about using a CSV parser.

like image 40
Alexis C. Avatar answered Nov 13 '22 00:11

Alexis C.