0

I am trying to read a CSV file, my approach is ti identify the column headers and read the data in it. apache has a library which is capable of doing this.

Below is my code

public class CSVItemReader {

    private File file;

    public CSVItemReader(File file)
    {
        this.file = file;
    }

    public List<Item> readFile()
    {
        try
        {
            Reader reader = new FileReader(file);
            CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT
                      .withHeader("Unit of Measure","Item Description")
                    .withIgnoreHeaderCase()
                    .withTrim());
            int i=0;
            for (CSVRecord csvRecord : csvParser) {


                String itemDescription = csvRecord.get("Item Description");
                String itemName = csvRecord.get("Unit of Measure");

                System.out.println(itemName+" : "+itemDescription);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return null;
    }
}

Unfortunately, the reader is only capable of reading the items under the first header item description. The rest is ignored, just blank is printed.

My CSV file also bit different, it is a file saved directly from MS Excel as a .CSV. So there are blank columns etc. I can't guarantee the first column is always blank, 3rd column is always blank etc. That is why I need the program to identify the titles in columns and read data in it.

I also have to say I can read them fine with index.

Below is a sample of my CSV.

,Item Description,,On Order,,BackOrd Qty,,On-hand Qty,,Item Name,,Vendor Name,,Active Price,,Item #,,Cost,,Avail Qty,,Department,,Attribute,,Size,,Margin %,,Ext Cost,,Ext Price,,Item Type,,Unit of Measure,,UPC,,Alternate Lookup,,Last Rcvd,,Reorder Point,,Regular Price,,Order Cost,,Margin,,Sale,,Wholesale,,MSRP,,1 Qty,,Custom price,,Dept Code,,Vendor Code,,Cust Ord Qty,,BackOrd Cost,,Last Sold,,Picture Assigned,,Manufacturer,,Shipping Weight,,Length,,Width,,Height,,Eligible for Rewards,,Creation Date,,Quick Pick Group,,Mobile,,

,Booster Pack ,,0,,0,,796,,83717845997,,,,1.99,,2977,,1.09,,796,,Financial Software,,,,,,45.1,,869.65,,"1,584.04",,Inventory,,,,0083733997,,08371734000,,9/26/2019,,0,,1.99,,1.07,,0.9,,1.79,,1.79,,0,,796,,1.79,,,,,,0,,0,,10/19/2019,,FALSE,,,,0,,0,,0,,0,,TRUE,,9/25/2019,,,,FALSE,,

How can I fix this please?

0

1 Answer 1

1

If you prefer to keep things simple and use a map (with the column name as key, and the column value as the value) then you could use CsvMapReader. There are examples on reading and writing with CsvMapReader on the Super CSV website.

Writing is quite similar.

ICsvMapReader mapReader = new CsvMapReader(
      new FileReader("foo.csv"), CsvPreference.EXCEL_PREFERENCE);
 try {
  final String[] headers = mapReader.getHeader(true);
  Map<String, String> row;
  while( (row = mapReader.read(headers)) != null) {
    for (String header : headers) {
      System.out.println(header + " is " + row.get(header));
    }
  }
} finally {
  mapReader.close();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.