有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何将文件中的值放入字符串中,并在Java中计算值?

我有一本书。csv文件,我了解如何在Java中读取它,我的问题是我希望能够将文件中的内容的值放入字符串中,然后读取字符串,以便计算这些值

Alice Jones,80,90,100,95,75,85,90,100,90,92 Bob
Manfred,98,89,87,89,9,98,7,89,98,78

这些都是值,现在我如何将每个名字旁边的所有整数相加,并创建一个平均值?我最大的问题是,当我读取文件时,它在一个While循环中,然后能够打印出这些行,就像它在控制台中看到的一样,但当我想打印While循环之外的值时,它说字符串不存在,因此如果其中没有任何内容,我就无法计算这些值

 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
 String line = strLine
 System.out.println (strLine);
 }
 //Close the input stream
 in.close();
 }catch (Exception e){//Catch exception if any
 System.err.println("Error: " + e.getMessage());

 }
 }
 }

共 (0) 个答案