Results 1 to 2 of 2
  1. #1
    latooka is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default ArrayIndexOutOfBoundsException Help

    I am receiving an ArrayIndexOutOfBoundsException error while I run the below code.

    Java Code:
    static String sRomans[] = {"I","V","X","L","C","D","M"};
    static int iRomans[] = {1,5,10,50,100,500,1000};
    while (pos <= s.length()) {
                    chr = s.substring(pos, 1);
                    p1 = Arrays.asList(sRomans).indexOf(chr);
                    subSum += iRomans[p1];
                    if(pos != s.length()) {
                        nextChr = s.substring(pos + 1, 1);
                        p2 = Arrays.asList(sRomans).indexOf(nextChr);
                        if (p1 == p2){
                            pos++;
                        }
                        else {
                            Arabic += subSum;
                            subSum = 0;
                            pos++;
                        }
                    }
                    else {
                        Arabic += subSum;
                        pos++;
                    }
                }
    The exception is thrown on this line:
    Java Code:
    subSum += iRomans[p1];
    If I remove the follwing code from the loop everything runs like expected.

    Java Code:
    chr = s.substring(pos, 1);
    p1 = Arrays.asList(sRomans).indexOf(chr);
    subSum += iRomans[p1];
    Any help and/or ideas are greatly appreciated.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,406
    Rep Power
    20

    Default Re: ArrayIndexOutOfBoundsException Help

    Array indexes start at 0 and end at (length - 1). Not at (length).

    And please don't flatter yourself that this is an advanced question. I'm moving the thread to New to Java.

    Also, you appear to have abandoned the last thread you started. Not the best way to continue to get free help on a forum, that.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. what is ArrayIndexOutOfBoundsException ???
    By gauravmanral in forum New To Java
    Replies: 6
    Last Post: 07-16-2011, 07:45 AM
  2. value of ArrayIndexOutOfBoundsException: 20 ?
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 03-28-2011, 03:46 PM
  3. ArrayIndexOutOfBoundsException
    By Corey in forum New To Java
    Replies: 5
    Last Post: 02-02-2010, 01:25 AM
  4. ArrayIndexOutOfBoundsException
    By flaskvacuum in forum New To Java
    Replies: 6
    Last Post: 07-14-2009, 05:36 PM
  5. ArrayIndexOutofBoundsException help
    By filly444 in forum New To Java
    Replies: 9
    Last Post: 09-03-2008, 05:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •