为什么Apache Commons CSV会计算整个文件的总唯一值?

时间:2018-10-15 02:29:09

标签: java csv automated-tests apache-commons-csv

我在目录中有2个csv文件(district1.csv,district2.csv),每个文件都包含“ schoolCode”和“ students”列。 当我使用apache commons csv库读取两个csv文件时,我正在读取“ schoolCode”和“ students”列的不同值。 我想要发生的事情是,对于每个uniqueSchool,计算不重复学生的数量。

所以我想要的输出是:

//district1
[INFO ] [Logger] - The number of students in school 01-0003-002: 42
[INFO ] [Logger] - The number of students in school 01-0003-001: 77

//district2
[INFO ] [Logger] - The number of students in school 01-0018-004: 200
[INFO ] [Logger] - The number of students in school 01-0018-005: 100
[INFO ] [Logger] - The number of students in school 01-0018-002: 200
[INFO ] [Logger] - The number of students in school 01-0018-003: 300
[INFO ] [Logger] - The number of students in school 01-0018-008: 200
[INFO ] [Logger] - The number of students in school 01-0018-006: 537

我当前的输出(将每个学校的整个学区档案的学生总数加起来)是:

//district1
[INFO ] [Logger] - The number of students in school 01-0003-002: 113
[INFO ] [Logger] - The number of students in school 01-0003-001: 113

//district2
[INFO ] [Logger] - The number of students in school 01-0018-004: 1537
[INFO ] [Logger] - The number of students in school 01-0018-005: 1537
[INFO ] [Logger] - The number of students in school 01-0018-002: 1537
[INFO ] [Logger] - The number of students in school 01-0018-003: 1537
[INFO ] [Logger] - The number of students in school 01-0018-008: 1537
[INFO ] [Logger] - The number of students in school 01-0018-006: 1537

这是我的代码:

@Test
    public void getTotalNumberOfStudentsPerSchool() throws IOException {
        Set<String> uniqueSchools = new HashSet<>();
        Set<String> uniqueStudents = new HashSet<>();
        int numOfStudents;
        File[] files = DDF.listFiles(new FileExtensionFilter()); //read only csv files.
        String schoolCode;
        String students;
        CSVParser csvParser = null;
        for (File f : files) {
            uniqueSchools.clear();
            uniqueStudents.clear();
            CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase().withTrim();
            reader = Files.newBufferedReader(Paths.get(DDF + "\\" + f.getName()), StandardCharsets.ISO_8859_1);
            csvParser = CSVParser.parse(reader, csvFormat);
            for (CSVRecord column : csvParser) {
                schoolCode = column.get("School Code");
                uniqueSchools.add(schoolCode);
                students = column.get("NDE State ID");
                uniqueStudents.add(students);
            }
            for (String school : uniqueSchools){
                numOfStudents = uniqueStudents.size();
                Logger.info("The number of students in school " + school + ": " + numOfStudents);
            }
        }
        csvParser.close();
    }

我知道我缺少一些简单的东西。任何帮助,将不胜感激。预先感谢!

0 个答案:

没有答案