Results 1 to 10 of 10
Thread: Out of bounds error:
- 04-28-2011, 02:30 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Out of bounds error:
I have the following method, it takes in an x / y variable that correlate to a position on a 10x10 2d array, the method simply checks to see if a spot is empty, if it is, it needs to do one thing, if it's not, it undoes everything up to that point.
Here's my code
This generates if the values passed into it are 0,0 and 0,1 (which works) then testing at 0,0 - 0,1 - 0,2 which generates the following compiler output.Java Code:public void AddVert(int x, int y, int z, char Letter) { int i, j; for (i=0; i<z; i++){ if (Arrays[y][x]!=' '){ System.out.println("Oh dear, it seems as though there is a collision at location: "Location.ReverseLoc(y, x)+ "Please try another location"); for (j=0; j<i; j++){ if (i > 0){ y--; Arrays[y][x]=' '; } else if (i<=0){ //Null } } } else { Arrays[y][x]= Letter; y++; }
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
Oh dear, it seems as though there is a collision at location: a1Please try another location
at Arrays.AddVert(Arrays.java:21)
at Test.main(Test.java:152)
Java Result: 1
Contents of Test.main line 152
Value in x = 0, y= 0, health = 3, letter = 'S' (Char s).Java Code:Object.AddVert(x,y,health,letter);
Any help with my logic on this would be delightful.
- 04-28-2011, 02:36 PM #2
Have you stepped through this with a debugger to follow what's going on?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-28-2011, 02:52 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
- 04-28-2011, 06:28 PM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
First position in Array is 0, second positon is 1, third position is 2 and so on.Java Code:if (Arrays[y][x]!=' '){ for (j=0; j<i; j++){ if (i > 0){ y--;
If you set
Java Code:Array[0][0]
it reffers to very first position. Now when you set
Java Code:y--;
it means you are looking for
Java Code:Array[-1][0]
which is impossible, the y position can not be lower than 0
You must think of your logic once again...
- 05-01-2011, 01:20 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
The basic premise of the code is to be able to take any spot on a 10x 10 grid and be able to place / undo anything that might be placed then cause a collision.
In this case I am checking at spot 0 / 0. During the first pass through of this loop, i should be = to 0, but the spot Arrays[y][x] should not be equal to 0. For some reason
is triggering instead ofJava Code:if (i > 0){
And I cannot for the LIFE of me figure out why?Java Code:else if (i<=0){
- 05-01-2011, 01:50 AM #6
How come you provided a code that doesn't compile?
I mean...
System.out.println("Oh dear, it seems as though there is a collision at location: "Location.ReverseLoc(y, x)+ "Please try another location");
That line has invalid syntax. (No + before "Location".) Can we see the EXACT code you are using?
Other than that, I can only think that i>0 because i loops for any value <z, which is 3... so, i will be 0, 1, 2.
I'd like to be able to run your code, but I'm not sure what Arrays is (or the rest of the class).Last edited by Zack; 05-01-2011 at 01:52 AM.
- 05-01-2011, 02:12 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Here's the most recent revisions of the code :
BattleShip.java -- BattleShip.java - Pastebin.com
Boatloc.java -- Boatloc.java - Pastebin.com
Arrays.java -- Arrays.java - Pastebin.com
Boat.java -- Boat.java - Pastebin.com
- 05-01-2011, 03:12 AM #8
Ok, I see what you're driving at now. I hate working with boards; my checkers game drove me nuts trying to do this all right.
I'd rework the method, honestly. Try an approach like this (for the vertical; swap y for x when doing the horizontal); note that this is in pseudocode mostly:
Java Code:if (y <= (boardHeight - z)) { return; } // y is too large to fit the piece on the board int y2 = y, z2 = z; while (z2 > 0) { // for each health check if Arrays[y][x] == ' ', if not, you need to end the function, cause the piece can't go here y2++ z2-- } z2 = z; y2 = y; while (z2 > 0) { // same loop again; the first one was just to check if ALL squares were valid first Arrays[y][x] = boatLetter y2++ z2-- }
I think that kind of idea should do it. Basically, you're checking first to see it the Y value is valid--that is, is it small enough that the highest amount of positions it will cover are all on the board? If not, return the function--it won't work.
Next, check all the squares that the boat will cover. Are any occupied? If not, you have a collision (which you can say in a message), and the function won't work, so it has to return.
Finally, place the boat, using the same logic as the checking.
I'd make this a boolean function, so you can return false if it wasn't placed and true if it was. Then the user can place it again if he or she has to.
EDIT: You could also compact the logic in the while loop to be, "while (y2 < y + z)", but I'd make sure you can get everything working before you worry about compacting/devariablizing it.Last edited by Zack; 05-01-2011 at 03:16 AM.
- 05-01-2011, 09:45 PM #9
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
- 05-03-2011, 05:14 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
I'll be marking this as solved for now, since I found a simple solution to this. In case anyone ever looks at this in the future:
I changed the call to my method to be a bool switch and changed the placement method to a boolean rather than a void.
BattleShip.java
Arrays.JavaJava Code://Vertical if Orientation boolean addboat=false; if (iOrientation == 1){ // If orientation is vertical addboat= MyPlayers.AddBoatVert(x,y,SelectedBoat.health,SelectedBoat.letter);// Add boat letter at selected location if (addboat == false){ SelectedBoat.used -=1; }// addboat = false }// End vert orientation if
Java Code:public boolean AddBoatVert(int x, int y, int z, char boatLetter) { int i, j; boolean booladdboat=true; for (i=0; i<z; i++){ // while I < SelectedBoat.health (z), increment i if (Arrays[y][x]!=' '){ // if X / y do not = default value System.out.println("Oh dear, it seems as though there is a collision at location: "+Boatloc.ReverseBoatLoc(y, x)+ "Please try another location");// There is a collision at location x/y booladdboat=false; return booladdboat; } else { // otherwise.... Arrays[y][x]= boatLetter; y++; } }// End for return booladdboat; }
Similar Threads
-
Out of Bounds Array...sometimes
By lampposteffect in forum New To JavaReplies: 12Last Post: 04-21-2011, 07:10 PM -
problem with array error index out of bounds exception
By cn23271 in forum New To JavaReplies: 4Last Post: 01-31-2011, 08:20 PM -
Array index out of bounds error
By blackstyle18 in forum New To JavaReplies: 3Last Post: 12-28-2010, 02:37 AM -
array going out of bounds?
By jabo in forum New To JavaReplies: 9Last Post: 04-02-2010, 10:08 AM -
Inconsistent out-of-bounds error
By geforce2000 in forum AWT / SwingReplies: 7Last Post: 12-23-2009, 08:27 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks