0

I am trying to figure out why my code won't allow me to correctly print any values in the array that are above the average value. I looked at a stackoverflow example of another user with a similar issue but it still didn't make sense. I don't even understand the current output for the values above the average.

How do I print the values in the array that are above the calculated average of all the array values?

I'd appreciate the help.

My code:

public class Inequality 
{
    public static void main(String[] args) 
    {
        // list of incomes in thousands
         int[] income = {2,10, 532, 4, 53, 28, 291, 38, 6, 17, 73, 21};
         int sum = 0;
         int average;
         int aboveAverage = 0;

         for (int i = 0;i< income.length;i++)
         {
             sum = sum + income[i];
         }
         average = sum/income.length;
         System.out.println("Average of income array: " + average);

         for (int i = 0; i < income.length; i++)
         {
             if (income[i]>average)
             {
                 aboveAverage++;
             }
         }
         System.out.println("There are " + aboveAverage + " numbers above the average.");
         System.out.println("Range of numbers above the average: " + income);
    }
}

My output:

Average of income array: 89
There are 2 numbers above the average.
Range of numbers above the average: [I@15db9742
Aramza
  • 193
  • 7
  • 29
  • 2
    You are printing the whole array object. What makes you think `income` would be contain the numbers above the average? – OneCricketeer Nov 10 '16 at 00:54
  • You print the above-average values in a similar way to how you're counting them: With a loop, an `if` statement, and a `print` statement. Or just add a `print` statement to the loop you already have. – Andreas Nov 10 '16 at 01:01

2 Answers2

0

Name of arrays with be the base address so in last line

System.out.println("Range of numbers above the average: " + income);

printing name will just print some address of array not any value. So if you need to fetch value you have to print like this

income[<index>] eg. income[1] , income[2]

Now About you wish of printing all values which are greater than average. just print it where you are comparing with average. so here is the code.

    for (int i = 0; i < income.length; i++)
     {
         if (income[i]>average)
         {
             aboveAverage++;
             System.out.println("Number above the average: " + income[i]);

         }
     }

I hope it is helpful.

Avinash
  • 2,093
  • 4
  • 28
  • 41
  • A simple explanation would improve this answer. – Andreas Nov 10 '16 at 01:07
  • Code is self explanatory, I thought you already got how many numbers are there above average so another thing is like print them. All numbers can be printed inside loops with check icome[i]>average. – Avinash Nov 10 '16 at 01:18
  • If you want to do anything else. Please update your questions. – Avinash Nov 10 '16 at 01:20
  • 1
    Are you somehow under the *mistaken* impression that *I* asked the question? I'm the person who up-voted your answer as being useful, but I think the answer can be *more* useful for newbies with a little explanation, including a disclaimer that your output will be different that the expected output in the question, e.g. you re-print the leading text for every number, instead of a comma-separated list on a single line. – Andreas Nov 10 '16 at 01:22
-1

You are printing the data value (I think that is what it's called) you need to do...

System.out.println(Arrays.toString(array));

or for you

System.out.println(Arrays.toString(income));

This should work for you!

Bioflame
  • 7
  • 1
  • That just prints the whole array, I am looking for it to print the values in the array that are above the average calculated value. – Aramza Nov 10 '16 at 00:50
  • @Aramza Then you need to iterate and only print the ones above the average. Or filter them out into a new array. All you do know is total the ones above the average--why not print them at the same time? – Dave Newton Nov 10 '16 at 00:51