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

**URL:** <https://community.jenkins.io/t/failed-cast-java-lang-string-to-java-lang-integer/5604>\
**Category:** Using Jenkins\
**Tags:** question\
**Created:** [February 4, 2023, 4:25am UTC](https://community.jenkins.io/t/failed-cast-java-lang-string-to-java-lang-integer/5604 "2023-02-04T04:25:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashhhhf22](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@ashhhhf22](https://community.jenkins.io/u/ashhhhf22)\
**Post date:** [February 4, 2023, 4:25am UTC](https://community.jenkins.io/t/failed-cast-java-lang-string-to-java-lang-integer/5604/1 "2023-02-04T04:25:05Z")

</div>

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:

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

build()

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);`

```auto
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]

```

- Second option: `for (int number = 1; i < charCount; i++)`  
Something similar to solution given in [Cannot be cast to java.lang.Integer - #8 by Samuelcloud07](https://community.jenkins.io/t/cannot-be-cast-to-java-lang-integer/4505/8)

```auto
java.lang.ClassCastException
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.

---

<div class="post-metadata">

**Author:** ![MarkEWaite](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/markewaite/32/20_2.png) [@MarkEWaite](https://community.jenkins.io/u/MarkEWaite)\
**Post date:** [February 4, 2023, 4:37am UTC](https://community.jenkins.io/t/failed-cast-java-lang-string-to-java-lang-integer/5604/2 "2023-02-04T04:37:20Z")

</div>

> [@ashhhhf22](#):
>
> ```auto
> def value = "123456789 + abcdefghijklmnopqrstuvwxyz"
> charCount = value.length()
> 
> build()
> 
> 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"
> }
>         
> }
> }
> }
> 
> ```

How about:

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

```

---

<div class="post-metadata">

**Author:** ![halkeye](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/halkeye/32/8_2.png) [@halkeye](https://community.jenkins.io/u/halkeye)\
**Post date:** [February 4, 2023, 4:47am UTC](https://community.jenkins.io/t/failed-cast-java-lang-string-to-java-lang-integer/5604/3 "2023-02-04T04:47:43Z")

</div>

> [@ashhhhf22](#):
>
> - First option: `int number = Integer.parseInt(charCount);`
> 
> ```auto
> 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
> 
> ```

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

> [@ashhhhf22](#):
>
> ` if (number >= "16") {`

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