Barry_D_Dale
Barry_D_Dale

Reputation: 131

CSV reader picking up blank lines

I have a program that reads in a CSV file and stores the first and last lines as a JSONObject. My current implementation is skipping over the last line and reading in a null value as the data object.

What's the correct way to create/save a CSV file as to eliminate any white space or null values?

CSV

   TIME_STAMP,TOTAL,APPLES,ORANGES,PEARS,GRAPES,TANGERINES,PINEAPPLES,CARROTS,UNKNOWN 
    7/1/2015 4:00,19474,1736,275,8366,5352,3003,393,349,

Code

String firstLine = "";
String lastLine = "";

int count = 0;
if(reader != null){
    String aux = "";
    String lastLineMinusOne = "";
    while ((aux = reader.readLine()) != null) {
        if(count == 0)firstLine = aux;
            lastLineMinusOne = lastLine;
            lastLine = aux;
            count ++;
        }
        logger.info("Count = " + count);             
        String[] columns = firstLine.split(",");
        String[] data = lastLine.split(",");
        logger.info(firstLine);
        logger.info(lastLine);

Log

    2015-09-28 13:41:42,370 [ajp-0.0.0.0-8009-2] INFO  com.ChartData - Count = 3
    2015-09-28 13:23:27,745 [ajp-0.0.0.0-8009-3] INFO  com.ChartData - TIME_STAMP,TOTAL,APPLES,ORANGES,PEARS,GRAPES,TANGERINES,PINEAPPLES,CARROTS,UNKNOWN
    2015-09-28 13:23:27,745 [ajp-0.0.0.0-8009-3] INFO  com.ChartData -

Error

java.lang.ArrayIndexOutOfBoundsException: 10
    at com.ChartData.getCSV(ChartData.java:75)

Line 75 -> jObject.put("val", data[i]);

Upvotes: 0

Views: 2728

Answers (2)

user1516873
user1516873

Reputation: 5193

Usually, using some good library is a best way, if you do it not for education. CSV looks simple, until you face text in quotes, escape characters, different line endings and so on.

In your case you can use apache commons.csv

   <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-csv</artifactId>
        <version>1.0</version>
   </dependency>

and short usage example, note withIgnoreEmptyLines(true):

final CSVFormat format = CSVFormat.DEFAULT
                .withIgnoreEmptyLines(true)
                .withDelimiter(',');
CSVParser parser = CSVParser.parse(file, Charset.forName("UTF-8"), format);
Iterator<CSVRecord> iterator = parser.iterator();

Upvotes: 1

Jordi Castilla
Jordi Castilla

Reputation: 26961

You can:

  1. parse line when readed as described here:

    while ((aux = reader.readLine()) != null) {
        String auxTrimmed = aux.replaceAll("(?m)^[ \t]*\r?\n", "");
        // more code
    }
    
  2. If you experience this problem always, just ignore last line:

    String[] data = lastLineMinusOne.split(",");
    logger.info(lastLineMinusOne);
    

Upvotes: 1

Related Questions