Looking for numbers greater than average - why is my IF statement not working correctly?

I am testing a program with various text files containing randomly generated numbers. The Java program is structured to add these numbers together from a text file, take the average of these numbers and then (using the IF statement) find the numbers from the text file that are greater than the average by inserting the specified values ​​into ArrayList

and printing the average and ArrayList

as output. For some reason, however, when I run my program with a different text file (I tested two, one that worked and one that does not currently work). The results printed in the shell are wrong - most of the values ​​are larger than the average, but I get a few that are not, and at intervals of no more than 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 a 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

      

All my life I cannot understand why these values ​​slip. I tested this program on another text file with fewer input values ​​and everything worked fine. I am new to Java as this is my second program after Hello World program and I don't have extensive knowledge of Java syntax.

+3


source to share


3 answers


When you decide to add a number to the list The value(s) greater than the mean

, you base that decision on the partial average of the numbers processed so far. This is why you see below the final average in the output elements.

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 add it to your output list. However, if the following items are greater than 2, the final average may be higher than 2, so your result will be below the final average.



your code may be wrong in the other direction as well - a number close to the beginning of the input may be mistakenly considered below average, even if it is above the final average.

To get the correct output, you must have two iterations. The first will calculate the average and keep all input numbers, while the second will find the numbers above the average.

+7


source


You are comparing values ​​to the current average, not the final average. Comparison for the nth element does not take into account the n + 1th element and beyond.



+10


source


If you want this to work, you need to go through the inputs twice, once to compute the average and once to determine which ones are above it.

When you read in the first meaning, you don't know what the mean is, so you cannot decide if it will be bigger than it!

The key point is that the mean is a property of the inputs as a whole, so unless you look at all of them, you cannot know the mean.

+1


source







All Articles