I'm having trouble trying to figure out what exactly needs to be done in this scenario for the code to work properly. I need to output a text file containing a name and the average of a list of values on each line, the text files contains these things:
Carol,35.00,67.00,13.00
Steve,14.00,82.00,41.00,66.00
Sharon,56.00,42.00,28.00,70.00
Roy,80.00,105.00,55.00
Beatrice,20.00
How do I output the average for each line in this scenario? The code below is an example of a more simpler one with each line only containing one value, I just don't know how to modify the array list or the code to get the values I want.
import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;
public class AvgCalc {
public static void main(String[] args) {
File myFile = new File("C:\\Users\\Byoma\\Downloads\\Assignment1.txt");
try {
Scanner myScanner = new Scanner(myFile);
while (myScanner.hasNextLine()) {
String line = myScanner.nextLine();
String[] tokens = line.split(",");
String name = tokens[0];
String average = tokens [1];
System.out.println("Name: " + name + ", Average: " + average);
}
myScanner.close();
}
catch (Exception e) {
System.out.println("Error reading file: " + myFile.getAbsolutePath());
}
}
}