Question: We are going to write a program that takes in a list of numbers of unknown size from a file, and then outputs the average

  • We are going to write a program that takes in a list of numbers of unknown size from a file, and then outputs the average of these numbers.
  • This activity will help you practice using a loop to help you read data from a file.
  • Open up a new Java project and name it Averages
  • Now add the two import statements that are required for file I/O to the top of your program:

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

  • Next, lets create a new File variable at the top of main and pass it in the name of the file "nums.txt"

File infile = new File("nums.txt");

  • Then, let's Scanner to open this file.

Scanner input = new Scanner(infile);

  • Next, lets read in the numbers from the file. Since we are computing the average of the numbers, we need two pieces of information

1. The sum of the numbers

2. How many numbers there are

  • Remember: Average = sum / count
  • Therefore, as we read in the numbers from the file, we need to computer their sum and count how many there are.
  • We will therefore need two variables to keep track of this information. Add the following two variable declarations to the top of main:

double sum = 0.0;

int count = 0;

  • We will also need a variable to temporarily store each number as we read it in from the file. Add an additional variable declaration to the top of main like so:

double num;

  • Now, let's read in the numbers from the file and process them inside a loop. Add the following loop to your program:

while (input.hasNextDouble()) { num = input.nextDouble();

System.out.println("Processing the number: " + num);

sum += num; //adding the number to our running total for the sum

count++; //counting how many numbers are in the file }

  • Now, let's print the average to the console and output it to a file.
  • To print the average to the console, add the following line of code:

System.out.println("The average is: " + sum/count);

  • To output the average to a file, we need to open a new output file. Below the print statement, add the following line of code:

File outfile = new File("average.txt");

  • Then, we need to connect this file to an output stream.

PrintWriter output = new PrintWriter(outfile);

  • Finally, lets write the average to the file.

output.println("The average is: " + sum/count);

  • As a last step we need to close our input and output streams. Add the following lines of code to the bottom of main:

input.close();

output.close();

  • Let's create a file to test our program. Open up a new empty file in Eclipse and name file nums.txt. In the file, add a list of numbers like this:

10 20 30 40 50

  • Now, run the code and open up your file to make sure everything is working properly. Open up the file averages.txt and verify that you got the correct output.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!