0

I have created very simple function that should return the first array after splitting it with a char.

int add(String version){
    splitVersion = version.split('.')
    echo splitVersion[0]
}

I'm calling the function like that: add("1.0.0")

And right after that I"m receving this error:

java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0

It's seem to be that it's not acting like a String at all, and when I try to print version it's print the value that sent(1.0.0)

What could be the issue?

Thanks!

1 Answer 1

2

The split method receives a regex, so you should use it like this.

int add(String version){
   splitVersion = version.split("\\.")
   echo splitVersion[0]
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, do you have any clue why for add("1.0.0") the line echo splitVersion[2]+1return 01 instand of 1?
while trying to parseInt i'm getting java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.EchoStep.message expects class java.lang.String but received class java.lang.Integer
i dont know what echo does, try using return splitVersion[0] instead.
i'm trying to increment the last index by 1. and instand of getting 1, im getting 01 like concatination..
yes that is because splitVersion[0] is a String, so "0"+1="01"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.