Failed cast java.lang.String to java.lang.Integer

I’ve been working on a script for the past day but I keep encountering almost the same error. I’ve been trying multiple ways to cast from string to integer but Jenkins keeps complaining about it.

A snippet of code:

def value = "123456789 + abcdefghijklmnopqrstuvwxyz"
charCount = value.length()


def build() {
    node {
        stage("Char Count") {
            echo "Length of the Output: ${charCount}"
            //int number = Integer.parseInt(charCount); (option #1)
           //for (int number = 1; i < charCount; i++)       (option #2)     
            if (number >= "16") {
                echo "Output too long"
            } else {
                echo "Can be added to Trello"

The idea is that value contains the output of a script executed in another stage (not included in above snippet).

  • First option: int number = Integer.parseInt(charCount);
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: static java.lang.Integer.parseInt() is applicable for argument types: (java.lang.Integer) values: [39]
Finished: FAILURE

Can someone please help me figure out a way were Jenkins doesn’t complain about Failed cast class java.lang.String to class java.lang.Integer?

All I need is to be able to compare if charCount value is greater than a number, do something.

Thanks in advance.

How about:

if (charCount > 16) {
   echo "too long"
} else {
  echo "Not too long"

its saying you can’t parseInt an integer, why are you trying to convert an integer to an integer?

If a number is greater than the string 16 doesn’t make any sense. You want number >= 16, not number >= 16.

OMG! The quotes… All I had to do was remove the quotes, I can’t believe it.

Thank you so much!