0
public void readCases(String filename) throws IOException {
  records.clear();  //name of list is records

  try (Scanner input = new Scanner(new File(filename))){
    input.nextLine();

    while (input.hasNext()){
      String text = input.nextLine();
      String[] element = text.split(",");
      for(int i = 0; i<=3; i++){
        if(element[i].equals(' ')){
          throw new DatasetException("column missing in csv");
        }
      }

      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
      LocalDate date = LocalDate.parse(element[0], formatter);
      int europeCases = Integer.parseInt(element[1]);
      int asiaCases = Integer.parseInt(element[2]);
      int africaCases = Integer.parseInt(element[3]);

      CaseRecord caseRecord = new CaseRecord(date, europeCases, asiaCases, africaCases);
      addRecord(caseRecord);    //this adds caseRecords to records
    }
  }
}

I am trying to clear an old list, then trying to read a CSV file and loading the list into the list. I'm not really sure where I'm going wrong. The list contains 4 fields with types

  1. DateTime
  2. int
  3. int
  4. int

and is in the form

2020-10-08,4,41,0

The top line reads

Date,Europe,Asia,Africa

and I want to skip this first line when loading the data into the list.

Abra
  • 11,631
  • 5
  • 25
  • 33
gretta
  • 43
  • 2
  • Does this answer your question? [Read CSV with Scanner()](https://stackoverflow.com/questions/14274259/read-csv-with-scanner) – Abra Mar 05 '21 at 10:04

1 Answers1

0

You can try Java lambda for reading lines of file, skip(1) to skip first line

        //read file into stream, try-with-resources
        try (Stream<String> stream = Files.lines(Paths.get(fileName))) {

            stream.skip(1).forEach(System.out::println);

        } catch (IOException e) {
            e.printStackTrace();
        }
ahll
  • 1,985
  • 1
  • 15
  • 21