Validate empty CSV lines

I read a CSV file and I don't know how to validate so that I don't take into account the lines empty and in this way do NOT thunder for exceptions. My code is as follows:

CsvReader usuarios_import = new CsvReader("H:/pagos.csv");
usuarios_import.readHeaders();
while (usuarios_import.readRecord())
 0
Author: Gusabio nava trujillo, 2016-10-14

2 answers

If you are using an instance of the class com.csvreader.CsvReader (of Java CSV - ), then you might want to call the method getRawRecord(), which returns the current line without process, which, in turn, you can validate that there is an empty line. That is:

while (csvReader.readRecord()) {
    String raw = csvReader.getRawRecord();
    if (!raw.trim().isEmpty()) {
        String col0 = csvReader.get(0);
        String col1 = csvReader.get(1);
        ...
    }
}
 1
Author: Paul Vargas, 2016-10-15 13:15:43

Actually your code is not understood because it is incomplete, however I once ran into the situation you mention in your basic question.

Suppose you already have a function that saves lines from a file and those lines are stored in a ArrayList<String> for example. I also assume that the number of columns in your CSV is constant, say, 5 columns.

Now as a mere didactic example let's process the list: ArrayList<String> Lista:

for (String linea : Lista){
    // Aqui se separan los campos separados por comas
    String[] campos = linea.split(","); 
    // Después se valida si la línea esta completa con sus 5 campos
    if(campos.lenght == 5){
        // Esta es una línea con datos
        // Tu lógica de negocio aqui...
    } else {
        // Esta línea no tiene datos y/o difiere de los 5 campos permitidos
        // Código para manejar tu línea vacía...
    }
}

In this case when method String.split(",") tries to separate an empty line, the resulting array is of size 1 whose only value is an empty string.

 0
Author: AngelAvila, 2016-10-14 22:07:32