Cannot be cast to java.lang.Integer

I am develop this script and have a issue with the valor varible I dont know how cast the valor to string to integer.

My code is for (int i = 1; i < “$valoclonint”; i++) {

                        println "valor de i "
                        println  i 
                    def projectNameclon = "${projectName}"+"C_${i}"

Error: java.lang.ClassCastException: org.codehaus.groovy.runtime.GStringImpl cannot be cast to java.lang.Integer

anything with a variable in it is a GStringImpl

Maybe try sprintf Groovy: Formatted printing with printf and sprintf not sure its whitelisted in jenkins though.

1 Like

Hi Halkeye ok, I got try this

The new error java.lang.ClassCastException
Finished: FAILURE

please post your new code snippet, its impossible to remote debug “it didn’t work”

1 Like

sorry men I am novota.

my problem now is my code is

def cantclones = readJSON file: “./Project/” + “${projectName}” + “/cantclones.json”
def valorclones = “${cantclones.num}”.trim()
Integer valoclonint = “${valorclones}”

the file json only have this : {
“num”:5
}

for (int i = 1; i < “${valoclonint}”; i++) this variable is

the msj error is
java.lang.ClassCastException
Finished: FAILURE

“5” is a string with 5 in it. I’m betting that is why groovy is complaing.

try

for (int i = 1; i < cantclones.num; i++) 

You shouldn’t need to trim a number, nor turn it into a string many times

Inow show me this error org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field java.lang.String num
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:425)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:409)

i was going by the documentation

try

for (int i = 1; i < cantclones['num']; i++) 

thanks man for the patience and the tips the solucion was for (int i = 1; i < (cantclones as Integer); i++)