1

I have a loop that initiates an array of strings then cuts them to give the desired information:

for (Element referenceId: referenceIds){
    ref[i] = referenceId.attr("href");
    String[] str = new String[24]; 
    str[i] = ref[i].substring(ref[i].lastIndexOf("listing-") + 8, ref[i].indexOf(".htm"));
    System.out.println(str[i]); 
    i++;
}

However I'm getting an error crashing the program:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 24
    at Jobs.main(Jobs.java:30)

What is this and how do I fix it? I've tried changing the array size to less but that didn't work.

2 Answers 2

1

Change this

String[] str = new String[24];

to this

String[] str = new String[ref.length];

Your ref array is probably having 25 or more items. So, as soon as the loop iterates over the 25th element (i.e. with index 24) you get an ArrayIndexOutOfBoundsException: 24.

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

1 Comment

It would help if you post the whole method code that covers the ref array.
1

You are iterating over referenceIds so you should use the size of this array/list for maybe it will change:

String[] str = new String[referenceIds.lenght];

You might also want to declare the String[] str outside the for loop.

Chears!

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.