Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: ArrayIndexOutOfBoundsException

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default ArrayIndexOutOfBoundsException

    So far, my loop will print the first element and then it errors out. Any suggestions would be great.

    public class Test {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args)
            {
    		Person a = new Staff(27, "Smith", "Gary", "Professor");
    		Person b = new Staff(12, "Doe", "Jane"," Professor");	
    		Person c = new Staff(6, "Anderson", "John", "Professor");
    		Person d = new Student(145, "Bee", "Corey", "Senior", "AMCS", "CS-250");
     
    		Person array[][] = {{a}, {b}, {c}, {d}};
     
    		for(int i=0; i<array.length; i++)
    		{
    			for(int j=0; i<array[i].length; j++)
    			{
    				System.out.print(array[i][j]);	//java.lang.ArrayIndexOutOfBoundsException: 1
    			}
    			System.out.println();
    		}
     
    	}
     
    }

  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: ArrayIndexOutOfBoundsException

    for(int j=0; i<array[i].length; j++)

    should be:

    for(int j=0; [b]j[/b]<array[i].length; j++)

    You accidentally put an i instead of a j in your condition. Fairly simple and understandable mistake.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Elementality (February 18th, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: ArrayIndexOutOfBoundsException

    Yikes, can't believe i didn't catch that. Thanks!

Similar Threads

  1. ArrayIndexOutOfBoundsException
    By NightFire91 in forum Exceptions
    Replies: 1
    Last Post: October 31st, 2010, 06:06 AM