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

Thread: Grid Pattern problem:

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Grid Pattern problem:

    My Code:

     
    import java.util.Scanner;
    import java.util.Random;
     
    public class turtle {
     
    	public static void main(String args[]){
     
    		Scanner input = new Scanner(System.in);
    		Random rand = new Random();
     
    		int grid[][] = new int[5][5], poschance, row = 0, column = 0, cont = 1;
    		while(cont == 1){
     
    			for(int i = 0; i <= 1000; i++){		
     
    				poschance = rand.nextInt(3 - 1 + 1) + 1;
    				switch(poschance){
     
    					case 1:
    						if(row == 4){
    							row--;
    						}
    						else{
    							row++;
    						}
    						break;
    					case 2:
    						if(column == 4){
    							column--;
    						}
    						else{
    							column++;
    						}
    						break;
    					case 3:
    						if((row == 4)&&(column == 4)){
    							row--;
    							column--;
    						}
    						else{
    							row++;
    							column++;
    						}
    						break;
     
    				}
    				grid[row][column]++;
     
     
    			}
     
    			row = 0;
    			column = 0;
     
    			while(row <= 4){
    				while(column <= 4){
     
    					System.out.printf("%d", grid[row][column]);
    					column++;
    				}
    				System.out.print("\n");
    				row++;
    			}
     
    			System.out.print("\nDo you want to continue?\n1. yes\n2.no\n");
    			cont = input.nextInt();
     
    		}
     
    	}
     
    }

    Runtime Error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at turtle.main(turtle.java:45)




    What I am trying to achieve:


    I want to see how randomly I can paint the grid of matrices 5x5 in 100 turns.


    Last word:


    Please help!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Grid Pattern problem:

    The problem is that you're trying to access index 5 (position 6) of one of your arrays, when you only have 5 positions (index 4) to access. Step through your program with a debugger, or at least at some print statements, to figure out what's going on.

    I'm not sure what you're trying to do though. Even without your error (hint: what happens when it hits 3 after hitting 1 or 2 a couple times), your logic doesn't seem to do what you said you were trying to do.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Grid Pattern problem:

    Thank you for your response. I looked at the problem again and came up with a sound logic(atleast I think its sound, but dunno whether it really is or not).

    This is my refined code:

    import java.util.Scanner;
    import java.util.Random;
     
    public class turtle {
     
    	public static void main(String args[]){
     
    		int grid[][] = new int[5][5], row = 0, column = 0, poschance;
     
    		Scanner input = new Scanner(System.in);
    		Random rand = new Random();
     
    		for(int i = 0; i <= 100; i++){
     
    			if((row == 0) && (column == 0)){
    				poschance = rand.nextInt(3 - 1 + 1) + 1;
    				System.out.printf("rand = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					row++;
    				case 2:
    					row++;
    					column++;
    				case 3:
    					column++;
    				}
    			}
    			else if((row == 0) && (column == 4)){
    				poschance = rand.nextInt(3 - 1 + 1) + 1;
    				System.out.printf("rand2 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					column--;
    				case 2:
    					row++;
    					column--;
    				case 3:
    					row++;
    				}
    			}
    			else if((row == 4) && (column == 0)){
    				poschance = rand.nextInt(3 - 1 + 1) + 1;
    				System.out.printf("rand3 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					row--;
    				case 2:
    					row--;
    					column++;
    				case 3:
    					column++;
    				}
    			}
    			else if((row == 4) && (column == 4)){
    				poschance = rand.nextInt(3 - 1 + 1) + 1;
    				System.out.printf("rand4 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					row--;
    				case 2:
    					row--;
    					column--;
    				case 3:
    					column--;
    				}
    			}
    			else if(row == 0){
    				poschance = rand.nextInt(5 - 1 + 1) + 1;
    				System.out.printf("rand5 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					column--;
    				case 2:
    					row++;
    					column--;
    				case 3:
    					row++;
    				case 4:
    					row++;
    					column++;
    				case 5:
    					column++;
    				}
    			}
    			else if(column == 0){
    				poschance = rand.nextInt(5 - 1 + 1) + 1;
    				System.out.printf("rand6 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					row--;
    				case 2:
    					row--;
    					column++;
    				case 3:
    					column++;
    				case 4:
    					row++;
    				case 5:
    					row++;
    					column++;
    				}
    			}
    			else if(row == 4){
    				poschance = rand.nextInt(5 - 1 + 1) + 1;
    				System.out.printf("rand7 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					column--;
    				case 2:
    					row--;
    					column--;
    				case 3:
    					row--;
    				case 4:
    					row--;
    					column++;
    				case 5:
    					column++;
    				}
    			}
    			else if(column == 4){
    				poschance = rand.nextInt(5 - 1 + 1) + 1;
    				System.out.printf("rand8 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					row--;
    				case 2:
    					row--;
    					column--;
    				case 3:
    					column--;
    				case 4:
    					row++;
    					column--;
    				case 5:
    					row++;
    				}
    			}
    			else{
    				poschance = rand.nextInt(8 - 1 + 1) + 1;
    				System.out.printf("rand9 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					row--;
    					column--;
    				case 2:
    					row--;
    				case 3:
    					row--;
    					column++;
    				case 4:
    					column--;
    				case 5:
    					column++;
    				case 6:
    					row++;
    					column--;
    				case 7:
    					row++;
    				case 8:
    					row++;
    					column++;
    				}
    			}
    			(grid[row][column])++;
     
    		}
     
    		row = 0;
    		column = 0;
     
    		while(row <= 4){
    			while(column <= 4){
     
    				System.out.printf("%d", grid[row][column]);
    				column++;
     
    			}
    			System.out.print("\n");
    			row++;
    		}
     
    	}
     
    }

    Error it shows:


    rand = 2
    rand9 = 8
    rand9 = 4
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at turtle.main(turtle.java:165)


    What i am trying to do is paint the matrix of 5x5 by randomly seeding the matrix to move the marker to adjacent tile for 100 turns which starts from position 00. to prevent the marker to exceed the threshold(i.e. 4 rows and 4 columns), i have added numerous if statements to hold the marker inside the 5x5 grid. and the print statement to print the grid after the 100 turns are finished. However to catch the error I also have print statement in each if to see whether the marker moves inside the grid or exceeds it. Please help me with this as I dont see why it is not computing as my logic says it should.

    Think of the problem this way.
    There is a 5x5 grid and lets say we let a mouse (or in this case a turtle), loose on the grid, so it starts moving.We dont want the mouse to go beyond the 5x5 grid so I placed few control statements to prevent it from doing so, where it checks whether the mouse is on the edges of the grid or not. and when the mouse has completed 100 steps we can see how it moved throughout the grid. However we have to keep in mind that, as mouse has a free will to move where it wants inside the grid, similarly I have set up a random integer for the variable position that the mouse can take "poschance", but within the grid. That's basically what I am trying to achieve here.

    p.s. I know the code is very lengthy and cumbersome to read but if there are any guru's who can tell me why i am getting the error, it would be great, because I dont see my array count exceeding the initialization. Thank you.
    Last edited by mick.bhattarai; December 29th, 2010 at 10:38 PM.

  4. #4
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Grid Pattern problem:

    Ok I was finally able to rectify the problem by including the "break"(yeah i am dumb enough to exclude it, :p). However I still have a major logical error which you can read below.

    so my code is:

     
    import java.util.Scanner;
    import java.util.Random;
     
    public class turtle {
     
    	public static void main(String args[]){
     
    		int grid[][] = new int[5][5], row = 0, column = 0, poschance, count = 1;
     
    		Scanner input = new Scanner(System.in);
    		Random rand = new Random();
     
    		for(int i = 0; i <= 9; i++){
     
    			System.out.printf("%d ", count);
     
    			if((row == 0) && (column == 0)){
    				poschance = rand.nextInt(3 - 1 + 1) + 1;
    				System.out.printf("rand = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					row++;
    					break;
    				case 2:
    					row++;
    					column++;
    					break;
    				case 3:
    					column++;
    					break;
    				}
    			}
    			else if((row == 0) && (column == 4)){
    				poschance = rand.nextInt(3 - 1 + 1) + 1;
    				System.out.printf("rand2 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					column--;
    					break;
    				case 2:
    					row++;
    					column--;
    					break;
    				case 3:
    					row++;
    					break;
    				}
    			}
    			else if((row == 4) && (column == 0)){
    				poschance = rand.nextInt(3 - 1 + 1) + 1;
    				System.out.printf("rand3 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					row--;
    					break;
    				case 2:
    					row--;
    					column++;
    					break;
    				case 3:
    					column++;
    					break;
    				}
    			}
    			else if((row == 4) && (column == 4)){
    				poschance = rand.nextInt(3 - 1 + 1) + 1;
    				System.out.printf("rand4 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					row--;
    					break;
    				case 2:
    					row--;
    					column--;
    					break;
    				case 3:
    					column--;
    					break;
    				}
    			}
    			else if(row == 0){
    				poschance = rand.nextInt(5 - 1 + 1) + 1;
    				System.out.printf("rand5 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					column--;
    					break;
    				case 2:
    					row++;
    					column--;
    					break;
    				case 3:
    					row++;
    					break;
    				case 4:
    					row++;
    					column++;
    					break;
    				case 5:
    					column++;
    					break;
    				}
    			}
    			else if(column == 0){
    				poschance = rand.nextInt(5 - 1 + 1) + 1;
    				System.out.printf("rand6 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					row--;
    					break;
    				case 2:
    					row--;
    					column++;
    					break;
    				case 3:
    					column++;
    					break;
    				case 4:
    					row++;
    					break;
    				case 5:
    					row++;
    					column++;
    					break;
    				}
    			}
    			else if(row == 4){
    				poschance = rand.nextInt(5 - 1 + 1) + 1;
    				System.out.printf("rand7 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					column--;
    					break;
    				case 2:
    					row--;
    					column--;
    					break;
    				case 3:
    					row--;
    					break;
    				case 4:
    					row--;
    					column++;
    					break;
    				case 5:
    					column++;
    					break;
    				}
    			}
    			else if(column == 4){
    				poschance = rand.nextInt(5 - 1 + 1) + 1;
    				System.out.printf("rand8 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					row--;
    					break;
    				case 2:
    					row--;
    					column--;
    					break;
    				case 3:
    					column--;
    					break;
    				case 4:
    					row++;
    					column--;
    					break;
    				case 5:
    					row++;
    					break;
    				}
    			}
    			else{
    				poschance = rand.nextInt(8 - 1 + 1) + 1;
    				System.out.printf("rand9 = %d\n", poschance);
    				switch(poschance){
    				case 1:
    					row--;
    					column--;
    					break;
    				case 2:
    					row--;
    					break;
    				case 3:
    					row--;
    					column++;
    					break;
    				case 4:
    					column--;
    					break;
    				case 5:
    					column++;
    					break;
    				case 6:
    					row++;
    					column--;
    					break;
    				case 7:
    					row++;
    					break;
    				case 8:
    					row++;
    					column++;
    					break;
    				}
    			}
    			(grid[row][column])++;
    			count++;
     
     
    		}
     
    		row = 0;
    		column = 0;
     
    		while(row <= 4){
    			while(column <= 4){
     
    				System.out.printf("%d", grid[row][column]);
    				column++;
     
    			}
    			System.out.println();
    			row++;
    		}
     
    	}
     
    }

    However now when it displays the grid(which i have use "while" loop for it), it only displays the first row(you can see my output below). Is there something I am doing wrong, or the marker itself is not moving to second row at all, if its the latter why ? BTW, I decreased the number of moves from 100 to 10, to make the error detection easier.

    output:

    1 rand = 1
    2 rand6 = 2
    3 rand5 = 2
    4 rand6 = 4
    5 rand6 = 2
    6 rand9 = 5
    7 rand9 = 6
    8 rand9 = 6
    9 rand6 = 1
    10 rand6 = 1
    01000


    as you can see its only displaying "01000" rather than;


    01000
    10000
    01120
    00120
    00000


    or something like this.


    EDIT: SOLVED

    Used for loop instead of while, however I am surprised why it didn't work with while loop. As you can see from the code below, and match it with above you can see, it's similar except for the conditional loop I used(which in this case if for loop). If you have an answer as to why it happened please leeme know.



     
    		for(row = 0; row <= 4; row++){
    			for(column = 0; column <= 4; column++){
    				System.out.printf("%d", grid[row][column]);				
    			}
    			System.out.print("\n");
    		}



    Please help.
    Last edited by mick.bhattarai; December 30th, 2010 at 01:46 AM.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Grid Pattern problem:

    In your while loop, you never reset your column counter, so it only entered the inner loop once.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. The Following User Says Thank You to KevinWorkman For This Useful Post:

    mick.bhattarai (December 31st, 2010)

Similar Threads

  1. Grid Computing, need to understand and learn
    By madcrazyboys in forum The Cafe
    Replies: 1
    Last Post: December 21st, 2010, 11:32 PM
  2. Randomizing cells in a grid
    By Flowbs in forum AWT / Java Swing
    Replies: 3
    Last Post: December 18th, 2010, 09:53 PM
  3. How to align the items in a form (grid layout)?
    By onlybarca in forum AWT / Java Swing
    Replies: 4
    Last Post: November 27th, 2010, 11:38 PM
  4. Help with regex pattern
    By b_jones10634 in forum Java Theory & Questions
    Replies: 4
    Last Post: September 24th, 2010, 03:59 PM
  5. Grid GUI Library
    By aussiemcgr in forum Java Theory & Questions
    Replies: 7
    Last Post: September 15th, 2010, 03:30 PM