This discussion is archived
9 Replies Latest reply: Apr 5, 2007 9:06 AM by JosAH RSS

Wierd ArrayIndexOutOfBoundsException

807599 Newbie
Currently Being Moderated
This one seems alright to look at but gives me an
ArrayIndexOutOfBoundsException. I can't see where I am crossing the array bounds.
/*package sathyaish.practice.utilities; 

I want to see how many expressions I can append to a single for-loop 
statement in Java. 

                for ( int i = 0; i < args.length; i++, sb.append( args[i] ), 
sb.append(delimiter)); 
*/

public class lfl /* long for loop */ 
{ 
        public static void main(String[] args) 
        { 
                if ( args.length == 0 ) 
                        System.out.println("Usage lfl arg1 [arg2 [arg3...]]"); 


                java.lang.StringBuffer sb = new StringBuffer(); 
                for ( int i = 0; i < args.length; i++, sb.append( args[i] ), 
sb.append(" ")); 
                System.out.println(sb.toString()); 
        } 
}
Test:
Prompt> java lfl Sathyaish is a good boy
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at lfl.main(lfl.java:14)
  • 1. Re: Wierd ArrayIndexOutOfBoundsException
    807599 Newbie
    Currently Being Moderated
    NEVER MIND

    Message was edited by:
    SomeoneElse
  • 2. Re: Wierd ArrayIndexOutOfBoundsException
    807599 Newbie
    Currently Being Moderated
    But I say:
    for(...; i < args.length;...)
    which iterates over the array 5 times from indexes 0 through 4. I don't see how that is a problem.

    Message was edited by:
    Sathyaish
  • 3. Re: Wierd ArrayIndexOutOfBoundsException
    3004 Newbie
    Currently Being Moderated
    Exception in thread "main"
    java.lang.ArrayIndexOutOfBoundsException: 5
    at lfl.main(lfl.java:14)
    On line 14 of Ifl.java, you're trying to access the 6th element (at index 5) of an array that does not have 6 elements.

    I'm not going to count your lines, but this looks like the culprit:
    i++, sb.append( args) 

    If there are 5 args, 0-4 will work fine. Then, after you've done the i++ that brings i to 5, you do the append, and there is no args[5]. Put the append inside the body of the loop.
  • 4. Re: Wierd ArrayIndexOutOfBoundsException
    791266 Explorer
    Currently Being Moderated
     for ( int i = 0; i < args.length; i++, sb.append( args[i] ),  sb.append(" ")); 
    That line sucks big time. the sb.appends should be in the body of the loop.

    Kaj

    Ps. @Op. Please don't try to be clever and write code that is hard to read and maintain. There are no benefits in trying to write it like you have done.
  • 5. Re: Wierd ArrayIndexOutOfBoundsException
    JosAH Newbie
    Currently Being Moderated
    IMHO you loop should look like this:
    for ( int i = 0; i < args.length; sb.append( args[i++] )) ...
    Besides being correct, my version looks more woody ;-)

    kind regards,

    Jos
  • 6. Re: Wierd ArrayIndexOutOfBoundsException
    3004 Newbie
    Currently Being Moderated
    But I say:
    for(...; i[b] < [/b]args.length;...)
    which iterates over the array 5 times from indexes 0
    through 4. I don't see how that is a problem.
    i becomes 5. That's what stops the loop body from executing.
    for (init; test; inc;) {
      body
    }
    
    is the same as
    
    init
    while (test) {
      body
      inc
    }
  • 7. Re: Wierd ArrayIndexOutOfBoundsException
    807599 Newbie
    Currently Being Moderated
    for ( int i = 0; i < args.length; i++, sb.append(
    args[i] ),  sb.append(" ")); 
    /code]
    
    That line sucks big time.   the sb.appends should be
    in the body of the loop.
    
    Kaj
    
    Ps. @Op. Please don't try to be clever and write code
    that is hard to read and maintain. There are no
    benefits in trying to write it like you have done.
    Yeah, I tried to edit my answer to say the same thing. Unfortunately I failed. The line was so out of the ordinary, I jumped to my first conclusion, and completely missed the actual problem.

    ~Tim
  • 8. Re: Wierd ArrayIndexOutOfBoundsException
    3004 Newbie
    Currently Being Moderated
    But I say:
    for(...; i < args.length;...)
    which iterates over the array 5 times from indexes 0
    through 4. I don't see how that is a problem.
    for (int ix = 0; ix < 3; ix++, System.out.println("in inc part ix=" + ix)) {
      System.out.println("In body ix=" + ix); 
    }
    
    In body ix=0
    in inc part ix=1
    In body ix=1
    in inc part ix=2
    In body ix=2
    in inc part ix=3
  • 9. Re: Wierd ArrayIndexOutOfBoundsException
    807599 Newbie
    Currently Being Moderated
    Thank you so much jverd and everyone else too.

    I had done exactly the same thing you suggested, jverd but I got an error. That is the subject of another separate topic, though -- one of the difference between statements and expressions.

    I got the reason and it is so profound. Thanks to all of you and to the help I got from this thread:

    http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/713e5edd1a3beda3/a87f874a35ee6d27?