2

I have this code to split the line to get key value pair, if value is missing it throw exection.

why its giving execption? what is the right way to split this key value pair line.

def lline="name="
def (key, value) = lline.split("=")

Error:

Caught: java.lang.ArrayIndexOutOfBoundsException: 1
java.lang.ArrayIndexOutOfBoundsException: 1

thanks

1 Answer 1

5

That's because lline.split("=") returns an array of 1 item ['name'], which you try and put the first item into key and the second into value

If you instead use the version of split that takes a limit:

def (key, value) = lline.split( "=", 2 )

it should work

Sign up to request clarification or add additional context in comments.

Comments

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.