Is to write a program project that asks the user to specify


Is to write a program (project) that asks the user to specify an input file and output file. Have the program read each number in the input file to calculate the average of all of the numbers. Then write that number to the output file along with "<== average".

To be clear - if the input file contained the numbers 5, 12, 3, 4 then the output file would only contain:

6.0 <== average

This is my assignment, right now I have my java code taking the smallest number of the file instead of the average number. What do I change it to to get the average of the numbers in the specified file?

Code:

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

public class FindAverage {

   public static void main(String[] args) throws FileNotFoundException {

       Scanner input = new Scanner(System.in);

  System.out.print("Please Enter the input file name: ");

  String inputFile = input.nextLine();

  File file = new File(inputFile);

   Scanner fileRead = new Scanner(file);

   double vals = Double.valueOf(fileRead.nextLine());

   while (fileRead.hasNextLine()) {

                double num = Double.valueOf(fileRead.nextLine());

                if( vals < num) {

                           vals = num;

                       } else {

                       }

            }

   System.out.println("Please Enter the output file name : ");

    String outputFile = input.nextLine();

    File fileout = new File(outputFile);

    PrintWriter pw = new PrintWriter(outputFile);

    pw.write(vals +" <== smallest number");

    pw.close();

  }

  }

Solution Preview :

Prepared by a verified Expert
Business Economics: Is to write a program project that asks the user to specify
Reference No:- TGS02892445

Now Priced at $10 (50% Discount)

Recommended (93%)

Rated (4.5/5)