I just started programming in Groovy, I have this error, and I wanted to see if anyone could help me with this.
java.lang.UnsupportedOperationException: Cannot use mod() on this number type: java.math.BigDecimal with value: 5
at Script1.hailstone(Script1.groovy:8)
at Script1$hailstone.callCurrent(Unknown Source)
at Script1.hailstone(Script1.groovy:11)
at Script1$hailstone.callCurrent(Unknown Source)
at Script1.hailstone(Script1.groovy:14)
at Script1$_run_closure1.doCall(Script1.groovy:1)
at Script1.run(Script1.groovy:1)
I have the following Groovy code
def list = [1,2,3].findAll{it-> hailstone(it)}
def hailstone(num){
if(num==1){
return 1;
}
println num
println num.mod(2)
if(num.mod(2)==0){
num = num/2;
return 1 + hailstone(num)
}else{
num = 3*num + 1
return 1 + hailstone(num)
}
}
and conclusion:
2
0
3
1
10
0
5
and then it unexpectedly causes an error on 5.mod (2).
Thanks in advance.
source
share