'How to read csv using column name in java

I have tried reading a column with its index using below code:

    int col1;
    String msg = null;
    int i = 0;
    String[] array = null;

    File file = new File(csvFileName);

    List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);

    for (String line : lines) {
        array = line.split(";", -1);
        // System.out.println(array[col1]);
        if (array[col1].equals("")) {
            // System.out.println("Cell is blank");
            break;

        } else {
            // System.out.println(array[col1].toString());
            i++;
        }

    }
    if (i < (array[col1].length())) {
        msg = "Invalid file";
        // System.out.println("Invalid File");
    } else {
        msg = "Valid file";
        // System.out.println("valid File");
    }

    return msg;

for eg.

|HEADER 1| HEADER 2| HEADER 3|
|A|B|C|
|D| |F|

As I am passing col index in col1, but I am not able to store the column header text in variable to print that which column is having blank value.

Is there any way I can read the column to validate blank cells and then print the column name in result saying "This column is having blank cell and hence file is invalid"

In above table, How to read columns with its name. Say if I pass HEADER 2 it should read all the row data and if it finds blank field



Solution 1:[1]

According to your input data example:

|HEADER 1| HEADER 2| HEADER 3|
|A|B|C|
|D| |F|

So you have a file composed from ha header line, where you have your column names, and the data.

To read the colimns name you need to parse the first row differently:

// field separator
String separator = ";";

//...
int i = 0;

Map<String, int> headers = new HashMap<>();

for (String line : lines) {
    if (i == 0) {
        String[] headLine = line.split(separator);
        for (int j = 0; j < headLine.length; j++) {
            headLine.put(headLine[j], j);
        }
        i++;
        continue;
    }
}

So you put each column name in your map and you save the index of the column.

The size of the map it is also the row len to check if you read all the expected fields.

Then you read the data:

int col1 = 1; // assign a valid column index
int fields = 0;

for (String line : lines) {

    if (array[col1].equals("")) {
        // System.out.println("Cell is blank");
        break;
    } else {
        // System.out.println(array[col1].toString());
        fields++;
    }
}

if (fields < headers.size()) {
    msg = "Invalid file";
    // headers[fields] is the name of the column with the empty cell 
}

In your example col1 is the index not the name of the column, but you never set it with a value, maybe this is one of the issues.

Solution 2:[2]

Below function will Read column name as parameter and output Valid if Column is Not empty and Invalid if column is empty


Input as in csv file

HEADER1;HEADER2;HEADER3;
A;;C;
D;E;F;

Solution:

public String fun(String col) throws IOException {
    String[] array = null;
    int Colx = -1; //Colx  is Column index
    File file = new File("test.csv");
    List < String > lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8); //reads all lines

    for (String line: lines) {

        array = line.split(";", -1); //split each line to array

        if (Colx != -1) { //Except first Row
            if (array[Colx].equals("")) { // and only Empty columns  
                return "Invalid";
            }
        } else { //only for first row // finding index of column matched

            for (String ar: array) {
                Colx++;
                if (ar.equalsIgnoreCase(col)) {
                    break;
                }
            }
        }
    }
    return "Valid"; //if Everything went well 
}

Solution 3:[3]

To get names of columns containing blank values in Java, the code will be rather long.

You can use SPL, the open-source Java package, to accomplish this. It is easy and only one line of code is enough:

A
1 =transpose(file("data.csv").import@w(;,";")).select(~.contain(null)).(~(1)/",This column is having blank cell and hence file is invalid")

SPL offers JDBC driver to be invoked by Java. Just store the above SPL script as validate.splx and invoke it in Java as you call a stored procedure:

…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st = con.prepareCall("call validate()");
st.execute();
…

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mario Santini
Solution 2 vikas dl
Solution 3