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 4 of 4

Thread: I need help explaining an OutofBounds error for a String grid I made?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question I need help explaining an OutofBounds error for a String grid I made?

    As my username suggests I need help with a lot of things. This grid I made is one of them. I get the following error message when I run the code below:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at gridArray.main(gridArray.java:12)
    ex.:
     
    class gridArray {
    	 public static void main(String[]args){
    	 //Creates a grid of integers
    	 int [][] grid = new int [4][2];
    	 int j,i;
    	 int v = 2;
    	 //Using i and j in a for loop This is going to create a grid with v as the constraint
    	 for (i=0;i<grid.length;i++){
    	 	for (j = 0; j < grid[i].length; j++);
    	 	{
    	 		grid[i][j] = v;
    	 		System.out.print(grid[i][j] + " ");
    	 		v--;
    	 	}
    	 	System.out.println("");
    	 }
    	 }
     }

    Therefore I was wondering if there was anything blatantly obvious that I did wrong?


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: I need help explaining an OutofBounds error for a String grid I made?

    Yes, theres a ; behind the second for loop, i.e. it just increments j.
    If you declare the indexes in the for loop you had gotten a compilation error:
     int [][] grid = new int [4][2];
    	 // int j,i;
    	 int v = 2;
    	 //Using i and j in a for loop This is going to create a grid with v as the constraint
    	 for (int i=0;i<grid.length;i++){
    	 	for (int j = 0; j < grid[i].length; j++)//;
    	 	{
    	 		grid[i][j] = v;
    	 		System.out.print(grid[i][j] + " ");
    	 		v--;
    	 	}
    	 	System.out.println("");
    	 }

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

    IneedHelpwithJava (April 11th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I need help explaining an OutofBounds error for a String grid I made?

    Thanks for the help, I can't believe it was that simple!

  5. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: I need help explaining an OutofBounds error for a String grid I made?

    You're welcome

Similar Threads

  1. I need help explaining the output.
    By Quecken in forum Java Theory & Questions
    Replies: 1
    Last Post: January 17th, 2013, 03:16 PM
  2. Help with explaining the code, pleaseeeeeee
    By katarina in forum Other Programming Languages
    Replies: 1
    Last Post: May 6th, 2012, 03:26 PM
  3. Please help, outofbounds error
    By iceyferrara in forum Exceptions
    Replies: 4
    Last Post: December 9th, 2011, 01:53 PM
  4. ArrayList to String/outOfBounds
    By Scotty in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 28th, 2011, 04:41 PM
  5. Outofbounds error
    By tendulkarfan4life in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2011, 11:20 AM

Tags for this Thread