# Thread: Greater Than the Average

1. ## Re: Greater Than the Average

Do NOT hardcode numbers like 500 in a for loop. Use the array's length. If you use the length if will work for any array, not just for one that has 500 elements.

That is the if statement for step 8).

2. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## Re: Greater Than the Average

I didn't hardcode the number, the teacher did. :)

3. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## Re: Greater Than the Average

AHH! I got it!!!!

Java Code:
```import java.util.Scanner;

public class Test2
{
public static void main(String[] args)
{
// declare variables
int sum;
int average;

// populate the array list
int[] list = {5,10,15,20,25};

// initialize sum
sum = 0;

// add all values in the list to sum
for (int j = 0; j < list.length; j++)
{
sum = sum + list[j];
}

// print the sum of the values in list
System.out.println ("The sum of the elements is " + sum);

// list that are greater than the average value of all the elements in the list.

average = sum / list.length;

for (int j = 0; j < list.length; j++)
if (list[j] > average)
{
System.out.println(list[j]);
}
}
}```

4. ## Re: Greater Than the Average

You should always use {} with for statements and if statements. The for statement on line 31 is missing the {}s
This is to prevent future problems when you are sloppy.

Now on to the part where you get the input from a user.

5. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## Re: Greater Than the Average

Okay. I will add them in. THANK YOU SO MUCH!!!!!

Page 3 of 3 First 123

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•