Java Topics Java I/O, while loop, summing for a total and average. References Te
ID: 672214 • Letter: J
Question
Java Topics Java I/O, while loop, summing for a total and average.
References Textbook – use the index for relevant topics; PowerPoint slides.
Specification
Using jGrasp | File | New | Plain Text or Notepad on a PC or TextEdit on a Mac, create an input text file which contains at least 10 numbers. At least two input lines should have more than one number on the same line. Your data should include at least three integers and at least seven real numbers (a real number contains a decimal point; include digits other than zero after the decimal point, like 87.65). An example of an input file is:
1 2.3 4.56
7 8.901
12
4.55 6.78
9.99
10.10
You will need to insert code that averages the numbers that are read from the input file. To do so, you’ll need to count the number of numbers and keep a running total. Output the individual number read and the running total sum to the output file and to the console in tabular form. Here is an example using the numbers above:
Number Running Total
1.0 1.00
2.3 3.33 Note the running sum is rounded to two places
4.56 7.86
7.0 14.86
etc.
After reading all the numbers, output the following to the output file and the screen with appropriate messages:
1. The total number of numbers read.
2. The sum of the numbers read to two decimal places.
3. The average of the numbers to two decimal places.
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class SampleSum {
public static void main(String[] args) {
// The name of the file to open.
String fileNameIn = "input.txt";
String fileNameOut = "output.txt";
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
java.io.File fin=new java.io.File(fileNameIn);
java.io.File fout=new java.io.File(fileNameOut);
FileReader fileReader = new FileReader(fin);
FileWriter fileWriter = new FileWriter(fout);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
double runningSum = 0.00;
int totalNoRead = 0;
System.out.println("Number Running Total");
while ((line = bufferedReader.readLine()) != null) {
// System.out.println(line);
String temp[] = line.split(" ");
for (int i = 0; i < temp.length; i++) {
if (!temp[i].isEmpty()) {
// System.out.println(temp[i]);
totalNoRead++;
double inVal = Double.parseDouble(temp[i]);
runningSum += inVal;
System.out.println("" + inVal + " "
+ Math.round(runningSum * 100.0) / 100.0);
}
}
}
// Always close files.
bufferedReader.close();
double avgRunningToal=(runningSum/totalNoRead);
bufferedWriter.write("1. The total number of numbers read. "+totalNoRead);
bufferedWriter.newLine();
bufferedWriter.write("2. The sum of the numbers read to two decimal places. "+(Math.round(runningSum * 100.0) / 100.0));
bufferedWriter.newLine();
bufferedWriter.write("3. The average of the numbers to two decimal places."+(Math.round(avgRunningToal * 100.0) / 100.0));
// Always close files.
// System.out.println(fout.getAbsoluteFile());
bufferedWriter.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Test 1
Number Running Total
1.0 1.0
2.3 3.3
4.56 7.86
7.0 14.86
8.901 23.76
12.0 35.76
4.55 40.31
6.78 47.09
9.99 57.08
10.1 67.18
input file :
1 2.3 4.56
7 8.901
12
4.55 6.78
9.99
10.10
output file :
1. The total number of numbers read. 10
2. The sum of the numbers read to two decimal places. 67.18
3. The average of the numbers to two decimal places.6.72
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.