Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Uditt Lamba

Thread: Help debugging the code please!

  1. #1
    Uditt Lamba is offline Member
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Angry "java.lang.ArrayIndexOutOfBoundsException: 11" Error, please help.

    The following is a code that I have been working on and it is Breaking when it tries to execute
    Java Code:
    P.Open(2, 1);
    It gives the following error: "java.lang.ArrayIndexOutOfBoundsException: 11" at
    Java Code:
    WQUUF.union(Map(i,j,n),Map(i-1, j, n));
    in Open() Method. The error only occurs for the value
    Java Code:
    P.Open(2, 1);
    if
    Java Code:
    Percolation P = new Percolation(10);
    I can't seem to understand why this is going out of bounds.

    PS: The WeightedQuickUnionFind class is a custom class and is error free.
    ================================================== =============================
    Java Code:
    public class Percolation {
    	static WeightedQuickUnionUF WQUUF;
    	Boolean[][] grid; 
    	static int n;
    	private boolean Validate (int i, int j) throws ArrayIndexOutOfBoundsException
    	{		
    			if(i<1||j<1||i>n||j>n)
    			{
    				return false;	
    				/*try{
    					throw new ArrayIndexOutOfBoundsException("e");
    				}catch(ArrayIndexOutOfBoundsException e){
    					StdOut.println("Exception caught");
    				}*/
    							
    			}else 
    			{
    				return true;
    			}	
    	}
    	
    	static int Map(int i, int j, int N)
    	{
    		int t;
    		t=(i-1)*N+j;
    		return t;
    	}
    	public Percolation(int N)
    	{	
    		n=N;
    		WQUUF= new WeightedQuickUnionUF((N^2)+2);
    		grid = new Boolean[N+1][N+1];
    		for(int i=1;i<=N;i++)
    		{
    			for(int j=1;j<=N;j++)
    			{
    				grid[i][j] = false;
    			}
    		}
    		/*for(int x=1;x<=N;x++)
    		{
    			WQUUF.union(0, x);
    		}
    		for(int y=(N^2);y<=(N^2)-N+1;y--)
    		{
    			WQUUF.union((N^2)+1, y);
    		}*/
    		
    	}
    	public void Open(int i, int j)
    	{
    		if(Validate(i, j))
    		{
    			grid[i][j]=true;
    			if(i==1)
    			{
    				WQUUF.union(0, Map(i, j, n));
    			}
    			if(i==n)
    			{
    				WQUUF.union((n^2)+1, Map(i, j, n));
    			}
    			if(Validate(i-1,j)&& isOpen(i-1,j))
    			{
    				WQUUF.union(Map(i,j,n),Map(i-1, j, n));
    			}else if(Validate(i+1, j) && isOpen(i+1, j))
    			{
    				WQUUF.union(Map(i,j,n), Map(i+1, j, n));
    			}else if(Validate(i, j-1) && isOpen(i, j-1))
    			{
    				WQUUF.union(Map(i,j,n), Map(i, j-1, n));
    			}else if(Validate(i, j+1) && isOpen(i,j+1))
    			{
    				WQUUF.union(Map(i,j,n),Map(i, j+1, n));
    			}
    		}else {
    			StdOut.println("Out of bound at "+i+","+j);
    		}
    	}
    	public boolean isOpen(int i, int j)
    	{
    		if(grid[i][j]== true)
    			{
    				return true;
    			}
    		return false;
    	}
    	public boolean isFull(int i, int j)
    	{
    		if(WQUUF.connected(0, Map(i, j, n)))
    		{
    			return true;
    		}else
    		{
    			return false;
    		}
    	}
    	public boolean percolates()
    	{
    		if(WQUUF.connected(0, (n^2)+2))
    		{
    			return true;
    		}else
    		{
    			return false;
    		}
    	}
    	public static void main(String args[])
    	{
    		Percolation P = new Percolation(10);
    		P.Open(1, 1);
    		P.Open(1, 2);
    		P.Open(2, 1);
    		P.Open(1, 3);
    		P.Open(5,1);
    		//P.Open(6,1);
    		StdOut.println(P.isOpen(5, 1));
    		StdOut.println(WQUUF.connected(Map(1, 2, 10),0));
    		StdOut.println(P.isFull(2,1));
    		StdOut.println(n);
    	}
    	
    }
    
    }
    Last edited by Uditt Lamba; 09-02-2013 at 01:49 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Help debugging the code please!

    Quote Originally Posted by Uditt Lamba View Post
    Help debugging the code please!
    Please go through the Forum Rules with special reference to the third paragraph.

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

  3. #3
    Uditt Lamba is offline Member
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default Re: Help debugging the code please!

    Quote Originally Posted by DarrylBurke View Post
    Please go through the Forum Rules with special reference to the third paragraph.

    db
    OK, how do I edit the title now?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Help debugging the code please!

    Click 'Edit Post' then 'Go Advanced'

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

  5. #5
    Uditt Lamba is offline Member
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default Re: Help debugging the code please!

    Done. Made it better.
    DarrylBurke likes this.

Similar Threads

  1. Help needed debugging Random Color code for applet
    By rjavaprodev in forum Java Applets
    Replies: 2
    Last Post: 11-19-2011, 07:35 AM
  2. Replies: 2
    Last Post: 10-07-2011, 07:48 AM
  3. Debugging a Java Code which is run by a Shell Script
    By ArunJava in forum New To Java
    Replies: 0
    Last Post: 09-12-2011, 03:21 PM
  4. Replies: 0
    Last Post: 09-09-2011, 11:04 AM

Posting Permissions

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