Results 1 to 11 of 11
  1. #1
    pinotje's Avatar
    pinotje is offline Member
    Join Date
    Dec 2010
    Location
    Belgium
    Posts
    14
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pinotje View Post
    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. #3
    pinotje's Avatar
    pinotje is offline Member
    Join Date
    Dec 2010
    Location
    Belgium
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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 04:10 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #5
    pinotje's Avatar
    pinotje is offline Member
    Join Date
    Dec 2010
    Location
    Belgium
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pinotje View Post
    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. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    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. #8
    pinotje's Avatar
    pinotje is offline Member
    Join Date
    Dec 2010
    Location
    Belgium
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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! :)
    Thanks for your tip...

  9. #9
    pinotje's Avatar
    pinotje is offline Member
    Join Date
    Dec 2010
    Location
    Belgium
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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. #10
    pinotje's Avatar
    pinotje is offline Member
    Join Date
    Dec 2010
    Location
    Belgium
    Posts
    14
    Rep Power
    0

    Default

    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. #11
    dpspine420's Avatar
    dpspine420 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

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

Similar Threads

  1. How to count random numbers together?
    By krechlich in forum New To Java
    Replies: 2
    Last Post: 04-04-2011, 07:29 PM
  2. Replies: 11
    Last Post: 01-14-2011, 06:36 PM
  3. Replies: 2
    Last Post: 10-24-2010, 04:34 PM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  5. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 08:49 AM

Posting Permissions

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