有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

逐行比较CSV文件和Java

我的代码示例如下:

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);
    } 
}

我有一个单独的类叫做“Person”,它根据CSV文件的输入实例化一个对象

我的任务是准备一个输出,在一行中生成一个人的姓名、年龄、生日和店铺访问日期,但是,如果此人有多个店铺访问日期,我应该只在下一行中显示店铺访问日期

我怎样才能比较当前行和下一行,看它是否与该人的姓名相同? 此外,我不允许创建任何ArrayList/List

样品。CSV文件内容: Sample of .CSV file contents

多谢各位

人员类别:

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];
}
}

共 (1) 个答案

  1. # 1 楼答案

    这是一个经典的数据处理练习,让我想起了30多年前我学习COBOL编程的时候。基本算法如下:

    1. 从文件中读取下一个条目。在你的情况下,一个人的细节
    2. 如果刚刚读取的人员与上次读取的人员不同,则完成对最后一个人的处理,并开始处理新人员,并将最后一个人详细信息设置为新人员详细信息
    3. 如果刚刚阅读的人与上次阅读的人相同,则继续处理当前的人

    在您的例子中,如果两个Person具有相同的name,则它们是相同的
    根据上述算法,您需要保存最后的Person详细信息
    下面是实现java代码

    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();
            }
        }
    }
    

    请注意,您需要关闭Scanner。上面的代码通过try-with-resources实现了这一点

    使用您提供的示例数据,上述代码的输出如下所示

    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