I have the following in a dat file:
Obe 20 12 15 20 -1
Twitter 16 12 18 19 -1
Karah 30 15 12 3 30 -1
Cappy 12 12 13 15 14 -1
Monkey 17 16 15 14 14 -1
I am trying to write a Java program to read information from the file, display the original information (one line for each person) along with the person's average and announce who is the highest average. I can't figure out how to get the average to work. I think because the dat file has both String values and int values, it doesn't allow me to do what I have below. Also I have no idea how to display the highest average or to display what I have above. Can anyone help?
import java.util.*;
import java.io.*;
import java.math.*;
import java.text.NumberFormat; // Needed for number format class.
import java.util.Scanner; // Needed for the Scanner class.
public class NumberAnalysis
{
public static void main(String[] args) throws IOException
{
String filename; // To hold a filename.
String first;
int count = 0, num = 0;
long sum = 0;
double average = 0, square = 0;
// Create a Scanner object to read input.
Scanner keyboard = new Scanner (System.in);
// Get the filename.
System.out.print("Enter the filename: ");
filename = keyboard.nextLine ( );
// Open the file.
FileReader freader = new FileReader(filename);
BufferedReader inputFile = new BufferedReader(freader);
// Read the first line from the file.
first = inputFile.readLine ( );
while (first != null)
{
Scanner fileScan = new Scanner (filename);
while (fileScan.hasNext())
{
sum += fileScan.nextInt();
count++;
}
average = (double) sum / (double) count;
System.out.println("sum == "+sum);
System.out.println("avg == "+average);
first = inputFile.readLine( );
}
// Close the file.
inputFile.close ( );
}
}