简体   繁体   中英

Loops and File Statistics Java Program

I am have a difficult time writing this program. I am very new to JAVA and I have the basic understanding. But I can't seem to understand what it is asking me to do.

import java.text.DecimalFormat;
import java.util.Scanner;       
import java.io.*;           

public class StatsDemo
{
    public static void main(String [] args) throws IOException  
    {
        double sum = 0; 
        int count = 0;  
        double mean = 0; 
        double stdDev = 0;
        String line;
        double difference;  //difference between the value and the mean

        DecimalFormat threeDecimals = new DecimalFormat("0.000");
        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();   

Here are some of the lines from Number.txt that this program is supposed to be reading from.

87.5517
72.14015
88.24248
60.524
65.38684
94.48039
84.73287
84.74978
73.78996
  • You were asked to use a FileReader in the comments. Why do you use a PrintWriter ? One is for input (reading from a file), the other is for output (writing to a file).
  • You did the "priming" and the while loop correctly in task #5. Why didn't you do the same in task #4? You do not have a variable named inputFile there.
  • In both cases, close the reader, not the file behind it. It will close the file behind it automatically.
  • In order to print with the correct number of decimal places, you have to use the threeDecimals object you were supplied with. It is of type DecimalFormat. You format the string by using threeDecimals.format(number) .

In general, all of the methods available in all of the standard types are in the Java SE API Documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM