I have been practicing questions out of a textbook that I downloaded online, and I was doing a program in the book which asked to write a program that searches a file of numbers and displays the largest number, smallest number and the average of all the numbers. The question also said to use numbers of type double that are written using writeDouble.
I have been able to all of which was required, except getting the average. I know that what I attempted when I tried to get the average of the numbers is incorrect, but I wanted to know if something to the effect of what I did exists, or if not, how I could obtain the average of the numbers without using an array? Here is my code
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
System.out.println("Enter file name to be written");
String file=keyboard.next();
File fileName=new File(file);
try{
FileOutputStream fo=new FileOutputStream(fileName);
ObjectOutputStream output=new ObjectOutputStream(fo);
System.out.println("Enter numbers");
System.out.println("enter q to quit");
String number;
do
{
number=keyboard.next();
if(number.equals("q")){
output.writeUTF(number);
}
else{
double new_number = Double.parseDouble(number);
output.writeDouble(new_number);
}
}
while (!number.equals("q"));
if(number.equals("q")){
System.out.println("numbers were written to: "+fileName);
output.close();
fo.close();
}
}
catch(FileNotFoundException e){
}
catch(IOException e){
}
double num=0;
double num2=0;
double num_check=0;
double num3=999999999999999999999.99999;
int count=0;
System.out.println("Now the program starts");
try{
FileInputStream fi=new FileInputStream(fileName);
ObjectInputStream input=new ObjectInputStream(fi);
long file_length=fileName.length();
for(int i=0;i<file_length;i++){
num=input.readDouble();
if(num>=0){
System.out.println(num);
}
if(num>num_check){
num_check=num;
}
else if(num<num3){
num3=num;
}
count++;
//Here is my attempt at getting the average
double num_ave=num+num(i+1);
double average=num_ave/count;
System.out.println(average);
System.out.println("Average of all the numbers is: "+average);**
}
input.close();
fi.close();
}
catch(FileNotFoundException e){
}
catch(EOFException e){
System.out.println("Largest number: "+num_check);
System.out.println("Smallest number: "+num3);
}
catch(IOException e){
}
}
}