Results 1 to 8 of 8
Thread: Really stuck in java
- 07-12-2011, 10:35 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
Really stuck in java
Hi..I am pretty new to JAVA and im suppose to write a code that generates a maze...i have the code and i run it and i keep getting arrayoutofbound exception..here is the code.
Java Code:public class StudentMazeGenerator implements MazeGenerator { private static final int h2 = 0; private static final int w2 = 0; private boolean[][] currMazeLoc; private boolean visited; //private Object randomNumInt; int[] listArray = {0,1,2,3}; public Maze generateMaze(int width, int height, MazeGeneratorListener listener) { Maze mazeFactory = MazeFactory.createMaze(width, height); currMazeLoc = new boolean[width][height]; generatePath(mazeFactory, width, height, listener); return mazeFactory; } public void generatePath(Maze maze, int w, int h, MazeGeneratorListener listener) { // int[] listArray = new int[] {0,1,2,3}; currMazeLoc[w][h] = visited; listener.mazeModified(maze); while(!unCheckedCell(w, h)) { Random random = new Random(); int temp = random.nextInt(); if(temp == 0 && h != 0) { //check inside each of them to make sure ur not goin out of bound //ur at the end of the maze and u want to go east there is no more room...u get arrayindexoutofboundException if(!(currMazeLoc[w][h-1])) { maze.removeWallNorthOf(w, h); h--; listener.mazeModified(maze); generatePath(maze, w, h ,listener); } } if(temp == 1 && w != w2 - 1) { if(!currMazeLoc[w+1][h]) { maze.removeWallSouthOf(w, h); h++; listener.mazeModified(maze); generatePath(maze, w, h ,listener); } } if(temp == 2 && h!= h2 - 1) { if(!currMazeLoc[w][h+1]) { maze.removeWallEastOf(w, h); h++; listener.mazeModified(maze); generatePath(maze, w, h ,listener); } } if(temp == 3 && w != 0) { if(!currMazeLoc[w-1][h]) { maze.removeWallWestOf(w,h); h--; listener.mazeModified(maze); generatePath(maze, w, h ,listener); } } return; } } //intArray.length-1; //int temp = listArray[i] //create an array of the (0 to 3) for N, S , E, W //create a method that shuffles the array N, E, S, W public void shuffle() { Random random = new Random(); int[] listArray = new int[] {0,1,2,3}; for (int i=0; i<listArray.length; i++) { int randomNum = random.nextInt(listArray.length -1); int temp = listArray[i]; } } public boolean unCheckedCell(int w1, int h1) { if (( w1 == 0) && (h1 == 0)) return (currMazeLoc[w1+1][h1] && currMazeLoc[w1][h1+1]); else if((w1 == (w2 - 1)) && (h1 == (h2 - 1))) return (currMazeLoc[w1-1][h1] && currMazeLoc[w1][h1-1]); else if((w1 == 0) && (h1 == (h2 - 1))) return (currMazeLoc[w1][h1-1] && currMazeLoc[w1+1][h1]); else if (w1 == (w2 - 1) && (h1 == 0)) return (currMazeLoc[w1 -1][h1] && currMazeLoc[w1] [h1 +1]); else return (currMazeLoc[w1+1][h1] && currMazeLoc[w1][h1+1] && currMazeLoc[w1-1][h1] && currMazeLoc[w1][h1-1]); //return false; } public int nextInt(int n) { return (int)(0 + Math.random() * n); } }
- 07-12-2011, 10:46 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Post the exact error message please. Stack traces are helpful, they tell you what part of the code threw an exception/caused an error. Somewhere you are indexing into an array above the length, or below 0.
Also in this piece of code, there is no point to add 0. .5555555555555+ 0 is still the same, it can be changed toJava Code:public int nextInt(int n) { return (int)(0 + Math.random() * n); }
Java Code:return (int)(Math.random()*n);
- 07-12-2011, 10:53 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
Exception in thread "Thread-4" java.lang.ArrayIndexOutOfBoundsException: 10
at StudentMazeGenerator.generatePath(StudentMazeGener ator.java:39)
at StudentMazeGenerator.generateMaze(StudentMazeGener ator.java:31)
at MazeFrame$MazeGenerationThread.run(MazeFrame.java: 275)
- 07-12-2011, 11:05 PM #4
Look at the code at line 39. The value of the index in the array used on that line is 10 but the array does not have 11 elements. Check your logic to see how the index got past the end of the array.java.lang.ArrayIndexOutOfBoundsException: 10
at StudentMazeGenerator.generatePath(StudentMazeGener ator.java:39)
Add some printlns to show the index values as the code executes if you need to see when the indexes go bad.
- 07-12-2011, 11:27 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
So i made some changes..since i had my width and height mixed up in some places but i still get out of bound exception.
Java Code:public class StudentMazeGenerator implements MazeGenerator { private static final int h2 = 0; private static final int w2 = 0; private boolean[][] currMazeLoc; private boolean visited; //private Object randomNumInt; int[] listArray = {0,1,2,3}; public Maze generateMaze(int width, int height, MazeGeneratorListener listener) { Maze mazeFactory = MazeFactory.createMaze(width, height); currMazeLoc = new boolean[width][height]; generatePath(mazeFactory, width, height, listener); return mazeFactory; } public void generatePath(Maze maze, int currX, int currY, MazeGeneratorListener listener) { // int[] listArray = new int[] {0,1,2,3}; currMazeLoc[currX][currY] = visited; listener.mazeModified(maze); while(!unCheckedCell(currX, currY)) { Random random = new Random(); int temp = random.nextInt(); if(temp == 0 && currY != 0) { //check inside each of them to make sure ur not goin out of bound //ur at the end of the maze and u want to go east there is no more room...u get arrayindexoutofboundException if(!(currMazeLoc[currX][currY-1])) { maze.removeWallNorthOf(currX, currY); //h--; listener.mazeModified(maze); generatePath(maze, currX, currY-1 ,listener); } } if(temp == 1 && currY != 9) { //if ur on south u can go as far 9 so u need to check ur y coordinate to make sure ur not going more than the size. if(!currMazeLoc[currX][currY+1]) { maze.removeWallSouthOf(currX, currY); //h++; listener.mazeModified(maze); generatePath(maze, currX, currY+1 ,listener); } } if(temp == 2 && currX != 9) { if(!currMazeLoc[currX+1][currY]) { maze.removeWallEastOf(currX, currY); //h++; listener.mazeModified(maze); generatePath(maze, currX+1, currY ,listener); } } if(temp == 3 && currX != 0) { if(!currMazeLoc[currX-1][currY]) { maze.removeWallWestOf(currX,currY); //h--; listener.mazeModified(maze); generatePath(maze, currX-1, currY ,listener); } } return; } } //intArray.length-1; //int temp = listArray[i] //create an array of the (0 to 3) for N, S , E, W //create a method that shuffles the array N, E, S, W public void shuffle() { Random random = new Random(); int[] listArray = new int[] {0,1,2,3}; for (int i=0; i<listArray.length; i++) { int randomNum = random.nextInt(listArray.length -1); int temp = listArray[i]; } } public boolean unCheckedCell(int w1, int h1) { if (( w1 == 0) && (h1 == 0)) return (currMazeLoc[w1+1][h1] && currMazeLoc[w1][h1+1]); else if((w1 == (w2 - 1)) && (h1 == (h2 - 1))) return (currMazeLoc[w1-1][h1] && currMazeLoc[w1][h1-1]); else if((w1 == 0) && (h1 == (h2 - 1))) return (currMazeLoc[w1][h1-1] && currMazeLoc[w1+1][h1]); else if (w1 == (w2 - 1) && (h1 == 0)) return (currMazeLoc[w1 -1][h1] && currMazeLoc[w1] [h1 +1]); else return (currMazeLoc[w1+1][h1] && currMazeLoc[w1][h1+1] && currMazeLoc[w1-1][h1] && currMazeLoc[w1][h1-1]); //return false; } public int nextInt(int n) { return (int)(Math.random()*n); } }
error i get:
Exception in thread "Thread-3" java.lang.ArrayIndexOutOfBoundsException: 10
at StudentMazeGenerator.generatePath(StudentMazeGener ator.java:39)
at StudentMazeGenerator.generateMaze(StudentMazeGener ator.java:31)
at MazeFrame$MazeGenerationThread.run(MazeFrame.java: 275)
- 07-12-2011, 11:29 PM #6
Did you add printlns to show the indexes used on line 39?
The print out could help you fix your problem.
- 07-12-2011, 11:32 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
do i add that after the line 39...where do i have to do printlns?
- 07-12-2011, 11:34 PM #8
Similar Threads
-
Java Help please xD SO stuck
By Ethan in forum New To JavaReplies: 1Last Post: 03-19-2011, 12:54 PM -
help..stuck w/ a java programming assignment
By clemsontigers in forum New To JavaReplies: 15Last Post: 02-24-2011, 09:31 PM -
New to java and stuck
By xpd259 in forum New To JavaReplies: 2Last Post: 12-12-2010, 09:34 AM -
Stuck new to Java
By Jaguar1998 in forum New To JavaReplies: 3Last Post: 04-09-2010, 12:02 PM -
Kinda stuck of learning Java
By jurka in forum New To JavaReplies: 2Last Post: 02-14-2009, 04:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks