Forum Discussion

_ivanovich_'s avatar
_ivanovich_
Frequent Contributor
5 years ago
Solved

How to validate all element from an array are less than a value?

Hi,

say we have an array like this

def my_array = [ [45.26], [44.6],[47.25] ] 

def expected = 50 as Float

how do you validate that each element are less than 50

 

i tried this:

assert my_array .every {it < expected }

It throws: 

groovy.lang.GroovyRuntimeException: Cannot compare java.util.ArrayList with value '[45.26]' and java.lang.Float with value '50' error at line: ...

It's comparing this value [45.26] instead of this 45.26

 

i tried this:

assert my_array.max() < expected

 

 

  • richie's avatar
    richie
    5 years ago
    Hi _ivanovich_,

    I know youve resolved your issue, but i have a quick question if thats ok for my education really.

    In your array each value was separated by individual square brackets.....why was you doing that? I wouldve thought (without testing your code myself) that your original code wouldve worked if you didnt include the square brackets around each value.....???

    Nice one,

    Rich

2 Replies

  • _ivanovich_'s avatar
    _ivanovich_
    Frequent Contributor

    Found solution with:

    log.info my_array.sum().max()

    which returns the max of all array then i have to compare.

    please close this thread.

    • richie's avatar
      richie
      Community Hero
      Hi _ivanovich_,

      I know youve resolved your issue, but i have a quick question if thats ok for my education really.

      In your array each value was separated by individual square brackets.....why was you doing that? I wouldve thought (without testing your code myself) that your original code wouldve worked if you didnt include the square brackets around each value.....???

      Nice one,

      Rich