Write a program that will find the average of a list of numbers found in a file.
ID: 3819519 • Letter: W
Question
Write a program that will find the average of a list of numbers found in a file. No input validation required. Assume the input file will have the correct type of data. The program must ask the user for the name of the file. You must have a method with the following header: public astatic double deviation This method will take a File object as input and use File I/O to calculate and return the standard deviation of a series of numbers found inside of a file. You should create your own test files to test the functionality of your program.Explanation / Answer
// Note: Please provide thumbs-up if you like the solution
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Average {
public Average() {
// TODO Auto-generated constructor stub
}
public static double deviation(File inputFile) throws IOException
{
double num = 0;
int count = 0;
double sum = 0;
double deviation = 0;
Scanner scan = new Scanner(inputFile);
while(scan.hasNext())
{
count++;
num = scan.nextDouble();
sum = sum+num;
deviation = sum/count;
//System.out.println("Value: "+num +" " +count +" "+sum+" "+ deviation);
}
scan.close();
return deviation;
}
public static void main(String args[]) throws IOException
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the file: ");
String filename=sc.next();
File file = new File(filename);
double dev = deviation(file);
System.out.println("The standard deviation of the values in this file is: "+dev);
sc.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.