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.
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:
Code java:
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.
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:
Code java:
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.
Code java:
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.
Re: Grid Pattern problem:
In your while loop, you never reset your column counter, so it only entered the inner loop once.