csv文件和操作

csv file and manipulation

S.M.Tido,112,145,124
P.julio,178,145,133
Carey,92,100,123
Elain,87,92,92
Theodore,178,155,167

我已经阅读了上面的文本文件,并试图找出每一行中 3 个读数的平均值。但是我只能找到单个列的平均值,因为我的 for 循环逻辑不起作用。谁能告诉我如何找到每一行的平均值?

   import java.util.Scanner;
   import java.io.*;
    
    public class PatientDetails{
    
        public static void main(String[] args){
        
            String fileName = "patient.txt";
            File file = new File(fileName);
            
            try{
                Scanner inputStream = new Scanner(file);
                int sum = 0;
                int noOfReadings = 3;
                
                while(inputStream.hasNext()){
                    String data = inputStream.next();
                    
                    
                    String[] values = data.split(",");
                    int readings1 = Integer.parseInt(values[1]);
                    int readings2 = Integer.parseInt(values[2]);
                    int readings3 = Integer.parseInt(values[3]);
                    
                    
                    sum = readings1 + readings2 + readings3;
                    }   
                    
                inputStream.close();
                System.out.println("Average = "+sum/noOfReadings);
            }
            catch(FileNotFoundException e){
                e.printStackTrace();
            }
        }
    
    }

注:

Note : I have not learnt data structures in Java so I cannot use lists
In my code.

只需将 println() 移入循环,然后将 sum 改回 0

Scanner inputStream = new Scanner(file);
int sum = 0;
int noOfReadings = 3;

while (inputStream.hasNext()) {
    String data = inputStream.next();

    String[] values = data.split(",");
    int readings1 = Integer.parseInt(values[1]);
    int readings2 = Integer.parseInt(values[2]);
    int readings3 = Integer.parseInt(values[3]);

    sum = readings1 + readings2 + readings3;
    System.out.println("Average = " + sum / noOfReadings);
    sum = 0;
}

inputStream.close();