0

I have an array with the format province;capital.

provArray = new String[] { "Alberta;Edmonton", "British Columbia;Victoria", "Manitoba;Winnipeg", "New Brunswick:Fredericton",
            "Newfoundland and Labrador;St.John's", "Nova Scotia;Halifax", "Ontario;Toronto", "Prince Edward Island;Charlottetown",
            "Quebec;Quebec City", "Saskatchewan;Regina", "Northwest Territories;Yellowknife", "Nunavut;Iqaluit", "Yukon;Whitehorse",
            "Alabama;Montgomery", "Alaska;Juneau", "Arizona;Phoenix", "Arkansas;Little Rock", "California;Sacramento", "Colorado;Denver",
            "Connecticut;Hartford"};

Then I have a for loop that separates the provinces from the capital (before and after the ";"). Yet for some reason, I get the error, java.lang.ArrayIndexOutOfBoundsException: length=1; index=1.

for(int k = 0; k < bonusArray.length; k++){
    String[] split = bonusArray[k].split(";");
    String prov = split[0];
    String cap = split[1];

    if(prov.equals(answer)){
       bonusAnswer = cap;
    }
}

How can I fix this error?

Edit: Fixed, I had accidentally put : instead of ; for one of my array items.

1
  • Consider that this isn't the best way to store this data: define a class Province, which stores the name and capital as separate fields, that you initialize like new Province("Alberta", "Edmonton"). Commented Oct 25, 2018 at 20:40

5 Answers 5

3

There are values in the array which don't have ; (semicolon), such as New Brunswick:Fredericton

so this code

String[] split = bonusArray[k].split(";"); gives an array of length = 1

and this causes the exception String cap = split[1]; because you can only access split[0] (remember length = 1).

So, you have to make sure every element in the array have ; or you check the length of the split variable before accessing their values.

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

4 Comments

e.g. "New Brunswick:Fredericton".
@AndyTurner exactly!
thank you for that! Didn't realize, that seems to have fixed it!
@Revo you're welcome. Great you solved the problem! Remember to up-vote all answers which helped you and accept one for future readers to find these posts useful.
2

You have a typo in "New Brunswick:Fredericton", needs a semi-colon vs a regular colon

1 Comment

thank you for that! Didn't realize, that seems to have fixed
1
 String[] split = bonusArray[k].split(";");

You are simply assuming, you will always get split variable with 2 elements. In this case your input has some elements which don't return 2 elements upon split, split() not really returning 2 elements, which is why ArrayIndexOutOfBoundsException.

Always check for "array length" before accessing it's elements at specified index.

EDIT: "New Brunswick:Fredericton" calling split(";") on this string will not return 2 elements.

1 Comment

Which don't return two elements for instance?
0

"New Brunswick:Fredericton" contains no ";" . Try to embed the split() and the [0], [1] part in a try-catch block, to ensure that no OutOfBoundsException occours.

Comments

0

We can do this with multiple ways but best approach is to use split.length-1 instead of giving hard coded value as index. By using this, you will never get this exception even when array length is 1.

for(int k = 0; k < bonusArray.length; k++){
    String[] split = bonusArray[k].split(";");
    String prov = split[0];
    String cap = split[split.length-1];

    if(prov.equals(answer)){
       bonusAnswer = cap;
    }
}

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.