Answered step by step
Verified Expert Solution
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 # Add the file IO import statement here
import java.text.DecimalFormat;
import java.ioBufferedReader;
import java.ioFileReader;
import java.ioFileWriter;
import java.ioPrintWriter;
import java.ioIOException;
This class reads numbers from a file, calculates the
mean and standard deviation, and writes the results
to a file.
public class StatsDemo
TASK # Add the throws clause
public static void mainString args throws IOException
double sum ; The sum of the numbers
int count ; The number of numbers added
double mean ; The average of the numbers
double stdDev ; 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 Systemin;
String filename; The user input file name
Prompt the user and read in the file name
System.out.printlnThis program calculates
"statistics on a file
"containing a series of numbers";
System.out.printEnter the file name: ;
filename keyboard.nextLine;
ADD LINES FOR TASK # HERE
Create a FileReader object passing it the filename
FileReader readfile new FileReaderfilename;
Create a BufferedReader object passing FileReader object
BufferedReader buffer new BufferedReaderreadfile;
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
whileline null
Convert the line to a double value and add the
value to sum
sum sum Double.parseDoubleline;
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 sumcount;
ADD LINES FOR TASK # HERE
Reconnect FileReader object passing it the
filename
readfile new FileReaderfilename;
Reconnect BufferedReader object passing
FileReader object
buffer new BufferedReaderreadfile;
Reinitialize the sum of the numbers
sum ;
Reinitialize the number of numbers added
count ;
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
whileline null;
Convert the line into a double value and
subtract the mean
difference Double.parseDoubleline 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.sqrtsumcount;
ADD LINES FOR TASK # HERE
three decimal format
DecimalFormat threeDec new DecimalFormat;
Create a FileWriter object using "Results.txt
FileWriter outfile new FileWriterResultstxt;
Create a PrintWriter object passing the FileWriter object
PrintWriter printfile new PrintWriteroutfile;
Print the results to the output file
printfile.formatMean s Standard Deviation s
threeDec.formatmean threeDec.formatstdDev;
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.ioFileNotFoundException: Numbers.txt No such file or directory
at java.basejavaioFileInputStream.openNative Method
at java.basejavaioFileInputStream.openFileInputStreamjava:
at java.basejavaioFileInputStream.FileInputStreamjava:
at java.basejavaioFileInputStream.FileInputStreamjava:
at java.basejavaioFileReader.FileReaderjava:
at StatsDemo.mainStatsDemojava:
can somebody fix this code for me
the Numbers.txt file is in UsersfrDownloads location
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started