Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
have you tried starting with int i = 0; ? –  bgth Apr 6 at 6:50

2 Answers 2

up vote 3 down vote accepted

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

int i = 0;
share|improve this answer
    
I did try that, but my array starts at 1. Should I change the item names in the array to begin at 0? –  Grux Apr 6 at 6:56
1  
@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). –  Jason C Apr 6 at 15:43
    
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. –  Grux Apr 7 at 17:46

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++;
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.