I have an assignment where I am supposed to get a user input file with data in it, like below:
Billy Bob 98.6 98.6 98.6 100 200 300 400 500 600 700 800 900 110 220 330 440 550 660 770 880 990 111 222 333 444 555 666 777 888 999 average 97.6
William R. Bob 98.6 101.3 98.6 101.3
98.6 101.3
average
99.9
Willy
Bob
98 101.1 101.2 103.1
Alphonse Lucius Horatio
Williams Bob
99.1 99.2 99.3 99.4 99.5 99.6 99.7 99.8 98.1 98.2 99.9
avERagE 100.1
From there we have to format the data like below:
Enter name of file: b.txt
===========================================================================Billy Bob: 98.6 98.6 98.6 100.0 200.0 300.0 400.0 500.0 600.0
700.0 800.0 900.0 110.0 220.0 330.0 440.0 550.0 660.0
770.0 880.0 990.0 111.0 222.0 333.0 444.0 555.0 666.0
777.0 888.0 999.0
--Average: 491.3(*)
---------------------------------------------------------------------------William R. Bob: 98.6 101.3 98.6 101.3 98.6 101.3
--Average: 99.9
---------------------------------------------------------------------------Willy Bob: 98.0 101.1 101.2 103.1
--Average: 100.8
---------------------------------------------------------------------------Alphonse Lucius Horatio Williams Bob: 99.1 99.2 99.3 99.4 99.5
99.6 99.7 99.8 98.1 98.2
99.9
--Average: 99.2(*)
---------------------------------------------------------------------------File Statistics
---------------
Total patient records: 4
Total body temp readings: 51
Not only that but we have to calculate the "average" for a person's records and also find how many patients there are and total number of records. We also have to find the average for each person, barring those that have a "average" record in the file. If there is a average already we still have to calculate it and compare to the one in the file and see if it is correct or not.
For the most part I have the user input of a file and tokenizing the data done, but I need help/explanations on how to do the rest. I have attached what I have for code so far. It's not much(compared to what I need to do), so I was wondering if someone could explain what I need to do get the output results like the example output.
Here is what I have so far:
// Assignment 1, Jan 29
// read data from a user specified file, then print; name, numbers and average
import java.io.*;
import java.util.*;
public class Assign1
{
public static void main(String [] argas)
{
String filename = getFilename();
FileReader fr = null;
BufferedReader br = null;
double total = 0.0;
try
{
fr = new FileReader(filename);
br = new BufferedReader(fr);
String aline = br.readLine();
while (aline != null)
{
total = total + doublesFromLine(aline);
aline = br.readLine();
}
System.out.println("Total from file: " + total);
}
catch (Exception fx)
{
if (fx instanceof FileNotFoundException)
System.out.println("File not found, terminating");
else if (fx instanceof IOException)
System.out.println("readLine() failed..terminating");
System.exit(0);
}
}
// return total of all doubles in the string
private static double doublesFromLine(String s)
{
double k = 0.0;
StringTokenizer st = new StringTokenizer(s);
while(st.hasMoreTokens())
{
String ss = st.nextToken();
if(isDouble(ss))
{
k+=Double.parseDouble(ss);
}
System.out.print(ss + " ");
}
return k;
}
// determine if the string is a valid double string
private static boolean isDouble(String s)
{
try
{
double d = Double.parseDouble(s);
return true;
}
catch (NumberFormatException nfx)
{
return false;
}
}
// get the file name
private static String getFilename()
{
String filename = kbdInput("Enter file name: ");
return filename;
}
private static String kbdInput(String prompt)
{
String s = "";
System.out.print(prompt);
try
{
BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in));
s = kbd.readLine();
}
catch (IOException iox)
{
System.out.println("Lost the keyboard...");
System.out.println("...terminating");
System.exit(0);
}
return s;
} // end of kbdInput()
}