Question

import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...

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

/**
   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)
   {
      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();
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution

Code

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
This class reads numbers from a file, calculates the
mean and standard deviation, and writes the results
to a file.
*/

public class Main
{

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
ArrayList<Double> values = new ArrayList<>();

Scanner keyboard = new Scanner (System.in);
String filename;

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();
keyboard.close();
  
FileReader File = new FileReader(filename);
BufferedReader readFile = new BufferedReader(File);
line = readFile.readLine();
while(true){
   line = readFile.readLine();
   if(line == null)
       break;
   else{
       values.add(Double.parseDouble(line));
       sum += Double.valueOf(line);
   count += 1;
   }  
}
readFile.close();
System.out.println(count);
mean = sum / count;
  
FileReader File_read = new FileReader(filename);
BufferedReader read_File = new BufferedReader(File_read);
sum = 0; count = 0;
line = read_File.readLine();
while(true){
   line = read_File.readLine();
   if(line == null)
       break;
   else{
       difference = (Double.valueOf(line) - mean);
       sum += Math.pow(difference, 2);
       count += 1;
   }
}
read_File.close();
stdDev = Math.sqrt(sum / count - 1);
  
FileWriter write = new FileWriter("Result.txt");
PrintWriter output = new PrintWriter(write);
output.printf("Mean of %s = %.3f.\n\n", values.toString(), mean);
output.printf("Standard Deviation of %s = %.3f.",values.toString(), stdDev);
output.close();
}
}

Screenshot

--

all the best

Add a comment
Know the answer?
Add Answer to:
import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into...

    1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into an appropriate folder in your drive. a) Go through the codes then run the file and write the output with screenshot (please make sure that you change the location of the file) (20 points) b) Write additional Java code that will show the average of all numbers in input.txt file (10 points) import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • Write one JUnit test in Java for the following: Essay class: import java.util.Scanner; public class Essay implements IAn...

    Write one JUnit test in Java for the following: Essay class: import java.util.Scanner; public class Essay implements IAnswer{ private String question; public Essay(String q){ this.question = q; } //This function returns question text public String getQuestionText() {    return question; } //This function takes answer from user public void answer(String userAnswer) {    // Take care of answer } @Override public String getAnswer() { System.out.println(question); Scanner scan = new Scanner(System.in); System.out.print("Answer: "); String ans =scan.nextLine(); scan.close(); if(ans.length() <=140){ return ans; }else{ return...

  • make this program run import java.util.Scanner; public class PetDemo { public static void main (String []...

    make this program run import java.util.Scanner; public class PetDemo { public static void main (String [] args) { Pet yourPet = new Pet ("Jane Doe"); System.out.println ("My records on your pet are inaccurate."); System.out.println ("Here is what they currently say:"); yourPet.writeOutput (); Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the correct pet name:"); String correctName = keyboard.nextLine (); yourPet.setName (correctName); System.out.println ("Please enter the correct pet age:"); int correctAge = keyboard.nextInt (); yourPet.setAge (correctAge); System.out.println ("Please enter the...

  • Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent...

    Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent debugging * problems. Solve one at a time, uncommenting the next * one only after the previous problem is working correctly. */ public class FindTheErrors { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); /* * Problem 1 Debugging * * This problem is to read in your first name, * last name, and current year and display them in *...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public...

    // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • package rpsgamesimulation; import java.util.Random; import java.util.Scanner; /** * * @author cristy */ public class RPSGame {...

    package rpsgamesimulation; import java.util.Random; import java.util.Scanner; /** * * @author cristy */ public class RPSGame { private String userChoice, computerChoice;    public RPSGame() { userChoice = "rock"; computerChoice = "rock"; }    public String getUserChoice() { return userChoice; }    public String getComputerChoice() { return computerChoice; }    public void setUserChoice(String aUserChoice) { userChoice = aUserChoice; }    public void setComputerChoice(String aComputerChoice) { computerChoice = aComputerChoice; }    public String toString() { return "User Choice: " + userChoice + "...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT