Another java.lang.ArrayIndexOutOfBoundsException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vasil angelov
    New Member
    • Mar 2011
    • 8

    Another java.lang.ArrayIndexOutOfBoundsException

    Can anyone tell me why j gets the value of y, instead of 0 in the following snippet?
    Code:
    import java.util.Scanner;
    public class matrix 
    {
    	public int x,y;
    	public void init(int[][] matrix1)
    	{
    		int i;
    		int j;
    		Scanner input = new Scanner(System.in);
    		System.out.printf("Enter x:");
    		x=input.nextInt();
    		System.out.printf("Enter y:");
    		y=input.nextInt();
    		matrix1 = new int[x][y];
    		
    		for( i=0 ; i<x ; i++)
    		{	
    			for( j=0 ; j<y ; j++);
    			{
    				//System.out.println("enter:");
    				System.out.printf("%d %d",i,j);
    				matrix1[i][j]=0;
    			 
    			}
    		}
    	}
    }
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    At which point to you get the value of y for j? The inner for-loop will increase j until it's equal to y, but that's the idea, right? Or are you having problems understanding how a for-loop works?

    Comment

    • vasil angelov
      New Member
      • Mar 2011
      • 8

      #3
      I solved it.The problem was at line 18 - i haven't noticed the semi-column after the for-loop.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Ah, one of those mistakes you easily make but are difficult to find... Well done for doing so yourself! :-)

        Greetings,
        Nepomuk

        Comment

        Working...