Warm tip: This article is reproduced from stackoverflow.com, please click
csv file java

Comparing line by line of a CSV file Java

发布于 2020-04-08 23:43:13

A sample of my code is as follows:

File myFile = new File("C:\\Windows\\Temp\\fileName.csv");

try {
    Scanner myScanner = new Scanner(myFile);
    while(myScanner.hasNextLine()) {
        String data = myScanner.nextLine();
        String[] fields = data.split(",");
        name = fields[0];
        age = Integer.parseInt(fields[1]);
        birthday = fields[2]; 
        storeVisitDate = fields[3];
        }
        Person myPerson = new Person(name,age,birthday,storeVisitDate);
    } 
}

I have a separate class called "Person", which instantiates an object based on the input of the CSV file.

What I am tasked to do is to prepare an output that generates the name, age, birthday and store visit date of a person on one line, however, if this person has multiple store visit dates, I should only display the store visit date in the following line.

How am I able to compare the current line to the next line to see if it is the same name of the person? Also, I am not allowed to create any ArrayLists/Lists.

Sample of .CSV file contents: Sample of .CSV file contents

Thank you.

Person Class:

public class Person{
private String name;
private int age;
private String birthday;
private String dateOfVisit;

public Person(String data)
{
String[] info = data.split(",");
name = info[0];
age = Integer.parseInt(info[1]);
birthday = info[2];
dateOfVisit = info[3];
}
}
Questioner
royalducky28
Viewed
81
Abra 2020-02-01 16:03

This is a classic data processing exercise and reminds me of when I learned COBOL programming over 30 years ago. The basic algorithm is as follows:

  1. Read the next entry from the file. In your case the details of a person.
  2. If the person just read is different to the last person read then complete processing for the last person and start processing the new person and set the last person details to the new person details.
  3. If the person just read is the same as the last person read then continue handling the current person.

In your case two Persons are the same if they have the same name.
According to the above algorithm, you need to save the last Person details.
Below is the implementing java code.

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Person {
    private String name;
    private int age;
    private String birthday;
    private String dateOfVisit;

    public Person(String data) {
        String[] info = data.split(",");
        name = info[0];
        age = Integer.parseInt(info[1]);
        birthday = info[2];
        dateOfVisit = info[3];
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getBirthday() {
        return birthday;
    }

    public String getDateOfVisit() {
        return dateOfVisit;
    }

    public String toString() {
        return String.format("%s %d %s ", name, age, birthday);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        File myFile = new File("C:\\Windows\\Temp\\fileName.csv");
        Person lastPerson = null;
        int count = 0;
        try (Scanner myScanner = new Scanner(myFile)) {
            while (myScanner.hasNextLine()) {
                String data = myScanner.nextLine();
                Person p = new Person(data);
                if (lastPerson == null  ||  !lastPerson.getName().equals(p.getName())) {
                    if (count == 1) {
                        System.out.println(lastPerson.getDateOfVisit());
                    }
                    System.out.print(p);
                    count = 1;
                    lastPerson = p;
                }
                else {
                    if (count == 1) {
                        System.out.println();
                        System.out.println(lastPerson.getDateOfVisit());
                    }
                    count++;
                    System.out.println(p.getDateOfVisit());
                }
            }
            // Print the visit date of the last person in the CSV file.
            if (count == 1) {
                System.out.println(lastPerson.getDateOfVisit());
            }
        }
        catch (IOException xIo) {
            xIo.printStackTrace();
        }
    }
}

Note that you need to close your Scanner. The above code achieves this via try-with-resources

Using the sample data you provided, the output of the above code is as follows.

Aliza Burn 20 01/01/2000 
07/20/2018
08/15/2016
04/14/2014
Edward Chase 45 02/14/1974 
05/05/2009
06/20/2014
Penny Lane 15 06/20/2004 
03/13/2018
05/06/2018
Josef Hunt 89 09/28/1930 08/21/2019