在Java中的csv文件中对行进行分组

我是Java的新手,我必须解析一个.csv文件。该文件在每一行中包含学生的ID,他们通过的科目ID和他们通过的科目成绩。例如:


Student ID,Subject ID,Grade

1,A1-102,7

1,A1-103,6

1,A1-104,5

1,A1-108,9

2,A1-101,5

2,A1-105,7

我需要计算一个学生通过的类似于SQL's GROUP BYEg的课程的数量:SELECT count(*) FROM STUDENTS GROUP BY Student_ID;假设csv文件已打开并可以读取,是否可以为一个学生对多个条目进行分组?


我的代码:


csvFile = "C:\\Myfile.csv";


             try {


            br = new BufferedReader(new FileReader(csvFile));

            while ((line = br.readLine()) != null) {

              // what do i need to do here?

            }

        } catch (FileNotFoundException e) {

            System.out.println("File not found\n");

        } catch (IOException e) {

            System.out.println("An I/O exception has occured\n");

        } finally {

                if (br != null)

                try {

                    br.close();

                } catch (IOException e) {

                    System.out.println("File is already closed");

                }

            }

有什么想法吗?


慕森王
浏览 206回答 3
3回答

天涯尽头无女友

对于数据组织而言,拥有一个arraylist并不是最佳的解决方案。我附加了最后一个解决方案,以引入一个哈希表,该哈希表存储由学生ID标识的数组列表。有些事情是一样的,例如for循环需要确切的学生人数。BufferedReader br = null;&nbsp; &nbsp; //this is the master HashMap, a datastructure which points to n amount of separate arraylist objects.&nbsp; &nbsp; HashMap<String, ArrayList<String>> master = new HashMap<>();&nbsp; &nbsp; //x = 3 for demonstration purposes replace the value with the&nbsp;&nbsp; &nbsp; //actual number of students&nbsp;&nbsp; &nbsp; for(int x = 1; x <= 3; x++) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> result = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; br = new BufferedReader(new FileReader(csvFile.toString()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String StudentIDNeeded = Integer.toString(x);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (line.substring(0, 1).equals(StudentIDNeeded)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.add(line.substring(2).toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; master.put(Integer.toString(x),result);&nbsp; &nbsp; &nbsp; &nbsp; } catch (FileNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("File not found\n");&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("An I/O exception has occured\n");&nbsp; &nbsp; &nbsp; &nbsp; } finally {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (br != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; br.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("File is already closed");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("Hash Size:"+master.size());&nbsp; &nbsp; System.out.println("Hash Contents" + master.toString());}此代码块输出这两个字符串Hash Size:3Hash Contents{1=[A1-102,7, A1-103,6, A1-104,5, A1-108,9], 2=[A1-101,5],&nbsp;3=[A1-105,7, A1-101,5]}该解决方案应该通过利用哈希图中的许多数组列表来扩展到更大的数据集。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java