-1

I'm trying to read .csv file like below:

BufferedReader reader = Files.newBufferedReader(Paths.get("przedmioty.csv"));

CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader("Name", "Teacher", "Years").withIgnoreHeaderCase().withTrim());

for (CSVRecord csvRecord: csvParser) {
    // Accessing Values by Column Index

    String name = csvRecord.get(0);
    //Accessing the values by column header name
    String Teacher= csvRecord.get(1);
    String years = csvRecord.get(2);

    //Printing the record
    System.out.println("Record Number - " + csvRecord.getRecordNumber());
    System.out.println("Name: " + name);
    System.out.println("Teacher: " + Teacher);
    System.out.print("Years : " + years );
    System.out.println("\n\n");
}

That's how my .csv looks like

    Name            Teacher                 Years
    Math            Leszek Krauze           2
    X               Grimm Dostojowksi       3
    Y               Dimitriv Vladiskovic    4

I'm getting

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at org.apache.commons.csv.CSVRecord.get(CSVRecord.java:79)
at main.Task01.main(Task01.java:294)

I use apache commons csv.

Madplay
  • 1,027
  • 1
  • 13
  • 25
G. Dawid
  • 162
  • 1
  • 11

1 Answers1

-1

I have tried with the following code and it is working fine for me. You can also check. I am using Common CSV version 1.4. Please check below the code. I have modified the code below.

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;

import java.io.FileReader;
import java.io.Reader;

public class Test2 {
  public static void main(String[] args) throws Exception {
    Reader in =
        new FileReader(
            "przedmioty.csv");
    Iterable<CSVRecord> records =
        CSVFormat.TDF
    .withRecordSeparator("\r\n")
            .withFirstRecordAsHeader()
            .withHeader("Name", "Teacher", "Years")
            .withIgnoreHeaderCase()
            .parse(in);

    for (CSVRecord csvRecord : records) {
      String name = csvRecord.get(1);
      // Accessing the values by column header name
      String Teacher = csvRecord.get(2);
      String years = csvRecord.get(3);

      // Printing the record
      System.out.println("Record Number -> " + csvRecord.getRecordNumber());
      System.out.println("Name: " + name);
      System.out.println("Teacher: " + Teacher);
      System.out.print("Years : " + years);
      System.out.println("\n\n");
    }
  }
}

There is some problem with your mentioned tab separated CSV file. I provide below the github link so that you can run and test and it has also an csv file inside testdata folder. It is maven based project.

https://github.com/debjava/sample-tab-csv-parser Hope it will solve your problem.

Sambit
  • 7,625
  • 7
  • 34
  • 65
  • I have tested with comma separated file, if it does not work for a different csv, please let me know so that I can help you. Both of us will learn. – Sambit May 18 '19 at 20:44
  • Actually i'm still gettin' Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at org.apache.commons.csv.CSVRecord.get(CSVRecord.java:79) at main.Task01.main(Task01.java:301) – G. Dawid May 20 '19 at 15:56
  • Did you check the code which I provided ? I tested with comma separated csv file ? Do you have a different type of csv file ? – Sambit May 20 '19 at 15:58
  • For some reason my .csv is tab separated due to fact that i'm using excel. – G. Dawid May 20 '19 at 16:20
  • You can try now, I have provided all the details. – Sambit May 20 '19 at 18:05
  • Let me know whether it is working fine for you or not. – Sambit May 20 '19 at 18:13