Java中csv文件中的分组行
我是Java新手,必须解析Java中csv文件中的分组行,java,Java,我是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的groupby的方式计算学生通过的课程数,例如:SELECT count(*)FROM STUDENTS GROUP BY student\u ID假设csv
.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的groupby
的方式计算学生通过的课程数,例如:SELECT count(*)FROM STUDENTS GROUP BY student\u 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");
}
}
有什么想法吗
编辑:文件中的所有学生都通过了相应的科目。您可以像这样轻松地使用Java8来完成
Pattern comma = Pattern.compile(",");
try (Stream<String> stream = Files.lines(Paths.get("C:\\data\\sample.txt"))) {
Map<Integer, Long> numberOfLessonsPassed = stream.skip(1).map(l -> comma.split(l))
.map(s -> new Student(Integer.valueOf(s[0]), s[1], Integer.valueOf(s[2])))
.filter(s -> s.getGrade() >= 5)
.collect(Collectors.groupingBy(Student::getId, Collectors.counting()));
System.out.println(numberOfLessonsPassed);
} catch (IOException e) {
e.printStackTrace();
}
我使用了一个.txt
文件,假设您能够将其移植到.csv
文件。您可以像这样轻松地使用Java8
Pattern comma = Pattern.compile(",");
try (Stream<String> stream = Files.lines(Paths.get("C:\\data\\sample.txt"))) {
Map<Integer, Long> numberOfLessonsPassed = stream.skip(1).map(l -> comma.split(l))
.map(s -> new Student(Integer.valueOf(s[0]), s[1], Integer.valueOf(s[2])))
.filter(s -> s.getGrade() >= 5)
.collect(Collectors.groupingBy(Student::getId, Collectors.counting()));
System.out.println(numberOfLessonsPassed);
} catch (IOException e) {
e.printStackTrace();
}
我使用了一个.txt
文件,假设您能够将其移植到.csv
文件。这里有一个更详细的解决方案
package com.company;
import javax.swing.text.html.StyleSheet;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Main {
static String csvFile = "your path";
public static void main(String[] args) {
// write your code here
BufferedReader br = null;
ArrayList<String> result = new ArrayList<>();
//this x value serves as the upper limit for the
//number of students you wish to view
for(int x = 0; x <= 3; x++) {
try {
String line;
br = new BufferedReader(new FileReader(csvFile.toString()));
String StudentIDNeeded = Integer.toString(x);
while ((line = br.readLine()) != null) {
if (line.substring(0, 1).equals(StudentIDNeeded)) {
result.add(line.toString());
}
}
} 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");
}
}
System.out.println(result.toString());
}
}
我增加了一些额外的分数,比如第三个学生id用于测试
要更新要选择的学生人数,请更改for循环中的x值。这里有一个更详细的解决方案
package com.company;
import javax.swing.text.html.StyleSheet;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Main {
static String csvFile = "your path";
public static void main(String[] args) {
// write your code here
BufferedReader br = null;
ArrayList<String> result = new ArrayList<>();
//this x value serves as the upper limit for the
//number of students you wish to view
for(int x = 0; x <= 3; x++) {
try {
String line;
br = new BufferedReader(new FileReader(csvFile.toString()));
String StudentIDNeeded = Integer.toString(x);
while ((line = br.readLine()) != null) {
if (line.substring(0, 1).equals(StudentIDNeeded)) {
result.add(line.toString());
}
}
} 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");
}
}
System.out.println(result.toString());
}
}
我增加了一些额外的分数,比如第三个学生id用于测试
要更新要选择的学生人数,请更改for循环中的x值。出于数据组织目的,使用一个arraylist不是最佳解决方案。我已经附加了我的上一个解决方案来引入一个hashmap,它存储由学生ID标识的ArrayList。有些东西是相同的,例如for循环需要精确的学生数量
BufferedReader br = null;
//this is the master HashMap, a datastructure which points to n amount of separate arraylist objects.
HashMap<String, ArrayList<String>> master = new HashMap<>();
//x = 3 for demonstration purposes replace the value with the
//actual number of students
for(int x = 1; x <= 3; x++) {
try {
String line;
ArrayList<String> result = new ArrayList<>();
br = new BufferedReader(new FileReader(csvFile.toString()));
String StudentIDNeeded = Integer.toString(x);
while ((line = br.readLine()) != null) {
if (line.substring(0, 1).equals(StudentIDNeeded)) {
result.add(line.substring(2).toString());
}
}
master.put(Integer.toString(x),result);
} 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");
}
}
}
System.out.println("Hash Size:"+master.size());
System.out.println("Hash Contents" + master.toString());
}
此解决方案应通过在hashmap中使用多个arraylist来扩展到更大的数据集。出于数据组织目的,使用一个arraylist并不是最好的解决方案。我已经附加了我的上一个解决方案来引入一个hashmap,它存储由学生ID标识的ArrayList。有些东西是相同的,例如for循环需要精确的学生数量
BufferedReader br = null;
//this is the master HashMap, a datastructure which points to n amount of separate arraylist objects.
HashMap<String, ArrayList<String>> master = new HashMap<>();
//x = 3 for demonstration purposes replace the value with the
//actual number of students
for(int x = 1; x <= 3; x++) {
try {
String line;
ArrayList<String> result = new ArrayList<>();
br = new BufferedReader(new FileReader(csvFile.toString()));
String StudentIDNeeded = Integer.toString(x);
while ((line = br.readLine()) != null) {
if (line.substring(0, 1).equals(StudentIDNeeded)) {
result.add(line.substring(2).toString());
}
}
master.put(Integer.toString(x),result);
} 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");
}
}
}
System.out.println("Hash Size:"+master.size());
System.out.println("Hash Contents" + master.toString());
}
此解决方案应通过利用hashmap中的多个ArrayList来扩展到更大的数据集。创建Student类,使用br.readLine().strip().split(“,”)对每个数据集进行解析。
如何确定学生是否通过?基于什么标准?@RavindraRanwala csv文件只包含通过考试的学生。因此,他们的分数是>=5你根本没有尝试过任何事情,这又是一个“为我做家庭作业”类型的问题。不过我给你一个提示,使用字典进行分组计数。首先,学习如何使用API读取CSV。然后,一旦你有了这些,你就可以创建一个POJO,按student对你的结果进行“分组”。创建student类,用br.readLine().strip().split(“,”)解析每个结果,你如何确定学生是否通过了考试?基于什么标准?@RavindraRanwala csv文件只包含通过考试的学生。因此,他们的分数是>=5你根本没有尝试过任何事情,这又是一个“为我做家庭作业”类型的问题。不过我给你一个提示,使用字典进行分组计数。首先,学习如何使用API读取CSV。然后,一旦你有了这些,你就可以创建一个POJO来按学生“分组”你的结果。这个解决方案的问题在于我的语言环境,十进制字符是逗号;)所以我根据每个学生的ID绘制地图,然后为他们通过的每个科目在地图中添加一条新记录?对于一项旨在帮助学生理解java基本原理的作业来说,似乎有点复杂。ArrayList有效吗?例如,我将每个学生添加到arraylist中,然后根据他们的ID,将他们的信息保存在新的arraylist中,并计算新创建的列表中的元素数。这就是我使用Java8的原因,因为它本身就适合于Java8。这个解决方案的问题在于我的语言环境,十进制字符是逗号;)所以我根据每个学生的ID绘制地图,然后为他们通过的每个科目在地图中添加一条新记录?对于一项旨在帮助学生理解java基本原理的作业来说,似乎有点复杂。ArrayList有效吗?例如,我将每个学生添加到arraylist中,然后根据他们的ID,将他们的信息保存在新的arraylist中,并计算新创建的列表中的元素数。这就是我使用Java8的原因,因为它本身就适合于它。我可以把arraylist切成更小的碎片吗?例如,可以将ID为1的所有元素添加到新列表中,将ID为2的元素添加到另一个列表中,依此类推。使用常量是没有效率的,我的最大id大约是1500。我现在将提交一份新的提交文件来解决这个问题。我可以将arraylist切成小块吗?例如,可以将ID为1的所有元素添加到新列表中,将ID为2的元素添加到另一个列表中,依此类推。使用常量是没有效率的,我的最大id大约是1500。我现在将提交一份新的提交文件来解决这个问题。