1

I have been parsing html to get the title of a specific link. I am able to add this string to the string array but I want to add a string from a string array which resides in my strings.xml resource.

int i = 1;
String time = null;
for (Element title : rels) {
    time = getResources().getStringArray(R.array.times)[i];
    titleArray.add(time + title.attr("title"));
    i++;
}

Here is my strings resource:

<string-array name="times">
    <item name="1">12:00 am</item>
    <item name="2">12:30 am</item>
    <item name="3">1:00 am</item>
    <item name="4">1:30 am</item>
    <item name="5">2:00 am</item>
</string-array>

I'm getting this error in LogCat:

04-06 01:44:08.837: E/AndroidRuntime(30938): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=48; index=48

I have 48 strings in this string array, which is exactly the same number of strings I'm parsing from the html. I'm not sure why my app is force closing. Does anyone know?

1
  • have you tried starting with int i = 0; ? Commented Apr 6, 2014 at 6:50

2 Answers 2

3

Array indices start at 0 not 1. Change i to start at 0:

int i = 0;
Sign up to request clarification or add additional context in comments.

3 Comments

I did try that, but my array starts at 1. Should I change the item names in the array to begin at 0?
@Josh Your array starts at 0. Also, the "name" attribute on "item" doesn't do anything. See developer.android.com/guide/topics/resources/… (note that "item" has no attributes).
I realize now that the name attribute was superfluous. Trying to teach myself java and I don't realize things like all string arrays have their own indices (which begin at 0). Thank you for the comment and the link. It was very helpful.
1

your loop should start from 0 index

    int i = 0;
    String time = null;
    for (Element title : rels) {
        time = getResources().getStringArray(R.array.times)[i];
        titleArray.add(time + title.attr("title"));
        i++;
    }

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.