[英]How do I manipulate a CSV file?
Create a program in Java that will read the CSV file and compute the Final Grade of the student using the computation below: Lab Exercise * 30% + Long Quiz * 30% + Alternative Assessment * 40% = Final Grade Then, display the final grade of each student.用 Java 创建一个程序,该程序将读取 CSV 文件并使用以下计算计算学生的最终成绩: 实验练习 * 30% + 长测验 * 30% + 替代评估 * 40% = 最终成绩 然后,显示最终成绩每个学生的。
We will be using the java.io package and so far this is what I come up with我们将使用 java.io 包,到目前为止,这就是我想出的
import java.io.*;
public class ReadingFile {
public static void main (String[]args){
try{
File myFile = new File("Grades.csv");
BufferedReader br = new BufferedReader(new FileReader(myFile));
String allLines = "";
while((allLines = br.readLine()) != null){
String row [] = allLines.split(",");
double finalGrade = (Double.parseDouble(row[1])*.30) + (Double.parseDouble(row[2])*.30) + (Double.parseDouble(row[3])*.40);
}
}
catch(IOException err){
System.err.println("File not found.");
}
}
}
I know that there are a lot of missing code but this is all what I can do with the best of my knowledge and abilities.我知道有很多缺失的代码,但这是我能用我最好的知识和能力做的所有事情。
Assume your content of Grades.csv
is as below假设您的
Grades.csv
内容如下
35,91,24
53,63,73
13,23,33
Some tips一些技巧
Use try-with-resources statement to ensure that BufferedReader is closed at the end of the statement.使用try-with-resources语句确保 BufferedReader 在语句结束时关闭。
Java uses zero-based indexing, so you should use row[0]
to read the first score. Java 使用从零开始的索引,因此您应该使用
row[0]
来读取第一个分数。
Use a logger or e.printStackTrace()
to help you debugging.使用记录器或
e.printStackTrace()
来帮助您调试。
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader(new File("Grades.csv")))) {
String allLines;
while ((allLines = br.readLine()) != null) {
String[] row = allLines.split(",");
double finalGrade =
(Double.parseDouble(row[0]) * .30) + (Double.parseDouble(row[1]) * .30) + (Double.parseDouble(row[2]) * .40);
System.out.println(finalGrade);
}
} catch (IOException e) {
System.err.println("some error message");
e.printStackTrace();
}
}
The output will be输出将是
47.4
64.0
24.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.