I am testing a program with different text files containing randomly generated numbers. The Java program is built to add these numbers together from the text file, take the average of those numbers, and then (using an IF statement) finding the numbers from the text file that are greater than the average, putting said values into an ArrayList
, and printing the average and the ArrayList
as output. For some reason, however, when I run my program with a different text file (I tested using two, one of which worked, and the one that currently does not). The results printed in the shell are not correct - the majority of the values are greater than the average, but I get a few that are not, and by a margin of at most three.
Here is my code:
package homework.pkg23.average;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class Homework23Average {
public static void main(String[] args) {
ArrayList exes = new ArrayList ();
double x = 0;
double y = 0;
Scanner inputStream = null;
try {
inputStream = new Scanner (new File ("MyInput.txt"));
}
catch (FileNotFoundException e) {
System.out.println ("File not found, program aborted:");
System.exit (1);
}
int count = 0;
while (inputStream.hasNextDouble ()) {
count ++;
x = inputStream.nextDouble ();
y += x;
if (x > y/count) // x values greater than the mean
exes.add (x);
}
System.out.println ("The value(s) greater than the mean, "
+ y/count + ", are (is):");
exes.forEach (System.out::println);
inputStream.close ();
}
}
When running this from the file, I get an average of 79.67, but my output looks like this:
The value(s) greater than the mean, 79.67, are (is):
128.0
93.0
143.0
111.0
95.0
116.0
136.0
129.0
141.0
78.0 <-- NOTICE: value is less than the average
93.0
105.0
90.0
90.0
144.0
116.0
136.0
138.0
75.0 <-- NOTICE: value is less than the average
80.0
126.0
75.0 <-- NOTICE: value is less than the average
80.0
98.0
114.0
116.0
86.0
78.0 <-- NOTICE: value is less than the average
123.0
145.0
103.0
111.0
91.0
134.0
119.0
91.0
121.0
113.0
129.0
91.0
116.0
85.0
85.0
126.0
145.0
98.0
115.0
83.0
127.0
119.0
97.0
125.0
121.0
123.0
86.0
108.0
100.0
134.0
I cannot figure out for the life of me why these values are slipping through. I tested this program on another text file containing fewer input values and everything worked fine. I am new to Java as this is my second program after the "Hello World" program, and I do not have extensive knowledge of Java syntax.
When you decide if you add a number to the list of
The value(s) greater than the mean
, you base this decision on the partial average of the numbers processed till this point. That's why you see in the output elements lower than the final average.For example, suppose the first element is 1 and the second is 2. Then the average of the first two elements is 1.5, and since 2 > 1.5, you'll add it to your output list. However, if the next elements are larger than 2, the final average might be higher than 2, so your output will have a number lower than the final average.
your code can err in the other direction too - a number near the beginning of the input may be mistakenly considered lower than the average even though it's higher than the final average.
In order to get the correct output, you must have two iterations. The first would calculate the average and store all the input numbers, and the second would find the numbers higher than the average.