【发布时间】:2013-04-18 04:53:45
【问题描述】:
好的,所以我有这段代码来获取包含这些值的 .csv 文件。
Alice Jones,80,90,100,95,75,85,90,100,90,92
Bob Manfred,98,89,87,89,9,98,7,89,98,78
我想取名字,然后是相应的成绩,然后计算他们的平均值。我坚持的部分实际上是在文件中检索这些值,以便我可以实际使用它们。我将使用什么来读取字符串以便将整数提取出来?
import java.io.*;
import java.util.*;
public class Grades {
public static void main(String args[]) throws IOException
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("filescores.csv");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
【问题讨论】:
-
请不要使用 DataInputStream 读取文本。这是多余的和令人困惑的。请从您的示例中删除它,因为此错误代码被大量复制。