Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.Scanner; / / TASK # 1 Add the file I / O import statement here import java.text.DecimalFormat; import java.io . BufferedReader; import java.io .

import java.util.Scanner;
// TASK #1 Add the file I/O import statement here
import java.text.DecimalFormat;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
/**
This class reads numbers from a file, calculates the
mean and standard deviation, and writes the results
to a file.
*/
public class StatsDemo
{
// TASK #1 Add the throws clause
public static void main(String[] args) throws IOException
{
double sum =0; // The sum of the numbers
int count =0; // The number of numbers added
double mean =0; // The average of the numbers
double stdDev =0; // The standard deviation
String line; // To hold a line from the file
double difference; // The value and mean difference
// Create an object of type Scanner
Scanner keyboard = new Scanner (System.in);
String filename; // The user input file name
// Prompt the user and read in the file name
System.out.println("This program calculates "+
"statistics on a file "+
"containing a series of numbers");
System.out.print("Enter the file name: ");
filename = keyboard.nextLine();
// ADD LINES FOR TASK #2 HERE
// Create a FileReader object passing it the filename
FileReader readfile = new FileReader(filename);
// Create a BufferedReader object passing FileReader object
BufferedReader buffer = new BufferedReader(readfile);
// Perform a priming read to read the first line of
// the file
line = buffer.readLine();
// Loop until you are at the end of the file
while(line != null){
// Convert the line to a double value and add the
// value to sum
sum = sum + Double.parseDouble(line);
// Increment the counter
count++;
// Read a new line from the file
line = buffer.readLine();
}
// Close the input file
buffer.close();
// Store the calculated mean
mean = sum/count;
// ADD LINES FOR TASK #3 HERE
// Reconnect FileReader object passing it the
// filename
readfile = new FileReader(filename);
// Reconnect BufferedReader object passing
// FileReader object
buffer = new BufferedReader(readfile);
// Reinitialize the sum of the numbers
sum =0;
// Reinitialize the number of numbers added
count =0;
// Perform a priming read to read the first line of
// the file
line = buffer.readLine();
// Loop until you are at the end of the file
while(line != null);
// Convert the line into a double value and
// subtract the mean
difference = Double.parseDouble(line)- mean;
// Add the square of the difference to the sum
sum = sum +(difference * difference);
// Increment the counter
count++;
// Read a new line from the file
line = buffer.readLine();
// Close the input file
buffer.close();
// Store the calculated standard deviation
stdDev = Math.sqrt(sum/count);
// ADD LINES FOR TASK #1 HERE
//three decimal format
DecimalFormat threeDec = new DecimalFormat("0.000");
// Create a FileWriter object using "Results.txt"
FileWriter outfile = new FileWriter("Results.txt");
// Create a PrintWriter object passing the FileWriter object
PrintWriter printfile = new PrintWriter(outfile);
// Print the results to the output file
printfile.format("Mean =%s, Standard Deviation =%s
",
threeDec.format(mean), threeDec.format(stdDev));
// Close the output file
printfile.close();
}
}
This program calculates statistics on a file containing a series of numbers
Enter the file name: Numbers.txt
Exception in thread "main" java.io.FileNotFoundException: Numbers.txt (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
at java.base/java.io.FileInputStream.(FileInputStream.java:157)
at java.base/java.io.FileInputStream.(FileInputStream.java:111)
at java.base/java.io.FileReader.(FileReader.java:60)
at StatsDemo.main(StatsDemo.java:41)
can somebody fix this code for me.
the Numbers.txt file is in Users>fr132>Downloads location

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions

Question

Question 1 (a2) What is the reaction force Dx in [N]?

Answered: 1 week ago

Question

List and describe three contingency leadership theories.

Answered: 1 week ago