Search code examples
javacsvbufferedreader

How can I get column numbers in csv using buffered reader in java?


So here is what I am trying to do:

My csv has 5 columns

a b c d e

How can i get the column number of each column in the csv using java and also for that column using the column number I want to append data to the cell.I am new to this concept,so please suggest how I could move ahead.Thanks.


Solution

  • Use String#split() to process every line and collect the column values into an array. The (array index + 1) would give you the column number then or the value at column n would be at (n - 1) index.

    String line = "a,b,c,d,e";
    String[] values = line.split(",");
    
    System.out.println ("Value at column 4 = " + values[3]);