5

I am parsing a CSV file into my program, spliting the values at the , element, and it's working fine, except for when I have lines that have missing values.

The parser is working like this:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class CsvReader
{
    private static final String DELIMITER = ",";
    private final BufferedReader br;
    private final String path;

    public CsvReader(final String path) throws IOException
    {
        this.path = path;
        this.br = new BufferedReader(new FileReader(path));
    }

    public String[] nextLine() throws IOException
    {
        final String line = br.readLine();
        return (line == null) ? new String[0] : line.split(DELIMITER);
    }
}

The lines of data look like this (one line as an example):

J1024205,5028197000004,1,,00,20150603,,Accessories,Factory Test Article (m),ENG,010,110,5,T1,99,99,,,99,99,99,ZZ,ZZ,,5028197242053,30,35028197242054,6,,,OPZ848,3013607800239,OPZ848,,,,50,,

Most of the lines in the file complete with this: 50,85028197242127,8640

But on some lines, the data is missing, so it ends like this: 50,,

When the file is being processed, these lines are causing a java.lang.ArrayIndexOutOfBoundsException.

How can I best deal with this, if I know that the numbers of objects in the file will remain constant?

I've been told that I need to replace empty values with a null value.

2
  • before doing split, use line.replace for '' to 'null'. since u need to replace the empty values with null. Commented Aug 18, 2015 at 9:15
  • @JishnuPrathap He means null value not "null" string :) Commented Aug 18, 2015 at 11:24

4 Answers 4

15

From the Javadoc of String.split(regex)

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, in your case, when the string ends with ,,, empty strings wont be part of the resultant array.

To Fix: Use this variant of split

line.split(DELIMITER, -1);

This will include all trailing empty strings. So you won't get an exception.

Sign up to request clarification or add additional context in comments.

4 Comments

This is the best answer for this problem because an empty field should yield an empty String :)
@haywire No OP needs a null for empty values.
so, he uses the code of Codebender and replaces every "" by null. problem solved.
i have written it down in an answer in case it helps.
1

This code results in array elements that are null if the column was empty.

// ... rest of OP's code

public String[] nextLine() throws IOException
{
    final String line = br.readLine();

    if(line == null)
    {
        return null; 
    }

    String columns[] = line.split(DELIMITER, -1);

    for(int i = 0; i < columns.length; i++)
    {
        if(columns[i].isEmpty())
        {
            columns[i] = null;
        }
    }

    return columns;
}

Comments

0

You can this way also. Just call split method on the String returned with comma

public String replaceNullSplitLine(String line){
    if(line.endsWith(",")){
        line = line+"***";
    }
    line = line.replaceAll(",,", ",***,");
    return line;
}

Comments

-1

Check the length of the array before trying to use the last (and in that case non-existed) values

myArray.length

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.