# Thread: how to count numbers under 5.5

1. ## how to count numbers under 5.5

Hi all,

Java Code:
`double numbers[] = {9.8, 5.3, 6.2, 5.5, 5.4, 7.7, 8.1, 3.7, 1.8, 4.6};`
how can I count numbers thats 5.5 or lower?
in the example above it must be 5 numbers under 5.5

Thanks

2. Originally Posted by pinotje
Hi all,

Java Code:
`double numbers[] = {9.8, 5.3, 6.2, 5.5, 5.4, 7.7, 8.1, 3.7, 1.8, 4.6};`
how can I count numbers thats 5.5 or lower?
in the example above it must be 5 numbers under 5.5

Thanks
What do you think the steps should be? Can we see your attempt?

3. Originally Posted by Fubarable
What do you think the steps should be? Can we see your attempt?
I really don't know...

I have tried this:
Java Code:
```if(numbers[i] < 5.5) { count++ }
System.out.println ("counted: " +count);```
Which is certainly wrong...
Last edited by pinotje; 05-10-2011 at 05:10 AM.

4. You need two constructs for this to work. First you have to go through all of the numbers in the array -- what code structure will allow you to loop through each item in the array?

5. Originally Posted by Fubarable
You need two constructs for this to work. First you have to go through all of the numbers in the array -- what code structure will allow you to loop through each item in the array?

Do you mean this?:
Java Code:
`for(int i=0; i<numbers.length; i++)`

6. Originally Posted by pinotje
Do you mean this?:
Java Code:
`for(int i=0; i<numbers.length; i++)`
Yes, exactly, a for loop. You've posted the "control" portion of the loop, and the loop will also have a body that will be between curly braces, {}. In the loop body you'll use the if block, and then after the loop you'll print out the results.

Give it a try! :)

7. Beware when using floating point numbers in equality statements.
Java Code:
```class DoubleTrouble {
public static void main(String[] args) {
int count = 0;
double[] values = {1, 7.0, 5.4, 5.5, 5.6, 5.499999999999999999999999999999};
for(double d : values) {
if(d < 5.5) {
count++;
}
}
System.out.println(count);
}
}```
You would expect the output of the above code to be 3 but is in fact only 2.

8. Originally Posted by Fubarable
Yes, exactly, a for loop. You've posted the "control" portion of the loop, and the loop will also have a body that will be between curly braces, {}. In the loop body you'll use the if block, and then after the loop you'll print out the results.

Give it a try! :)

9. Originally Posted by Junky
Beware when using floating point numbers in equality statements.
Java Code:
```class DoubleTrouble {
public static void main(String[] args) {
int count = 0;
double[] values = {1, 7.0, 5.4, 5.5, 5.6, 5.499999999999999999999999999999};
for(double d : values) {
if(d < 5.5) {
count++;
}
}
System.out.println(count);
}
}```
You would expect the output of the above code to be 3 but is in fact only 2.
Wow Thanks!

This is exactly what I'm looking for!

10. Can anyone tell me the difference between:

Java Code:
`double[] numbers = new double[]{1.0, 1.5, 2.0, 2.5, 3.0, 3.5};`
,

Java Code:
`double[] numbers = {1.0, 1.5, 2.0, 2.5, 3.0, 3.5};`
and

Java Code:
`double numbers[] = {1.0, 1.5, 2.0, 2.5, 3.0, 3.5};`
I've tried all those 3 codes, and they work the same for me...
Are these codes just the same?

11. yes all 3 are ways to do the same thing, although the first one is redundant. I personally prefer
double[] numbers = {values};

#### Posting Permissions

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