Results 1 to 10 of 10
  1. #1
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default 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
    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++;
                }
    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.

    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
    Java Code:
    Object.AddVert(x,y,health,letter);
    Value in x = 0, y= 0, health = 3, letter = 'S' (Char s).


    Any help with my logic on this would be delightful.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    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!

  3. #3
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Have you stepped through this with a debugger to follow what's going on?
    Sad, simple truth here, I never learned how to use the debugger in netbeans. I can for my c++ class but I've never needed to (until now) for java / netbeans. Know of a tutorial for it?

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    272
    Rep Power
    4

    Default

    Java Code:
    if (Arrays[y][x]!=' '){
            for (j=0; j<i; j++){
                     if (i > 0){
                            y--;
    First position in Array is 0, second positon is 1, third position is 2 and so on.
    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...

  5. #5
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    Quote Originally Posted by milovan View Post
    Java Code:
    if (Arrays[y][x]!=' '){
            for (j=0; j<i; j++){
                     if (i > 0){
                            y--;
    First position in Array is 0, second positon is 1, third position is 2 and so on.
    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...
    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

    Java Code:
    if (i > 0){
    is triggering instead of

    Java Code:
    else if (i<=0){
    And I cannot for the LIFE of me figure out why?

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    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.

  7. #7
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    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).

    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

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    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.

  9. #9
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    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.
    I think that I understand what you're getting at here, I'll toss up my code tomorrow night and see if it both works and / or needs more help, thanks for the suggestion so far, and trust me, I TOTALLY feel your pain on working with a game board.... HUGE mistake.

  10. #10
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    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
    Java 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
    Arrays.Java
    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

  1. Out of Bounds Array...sometimes
    By lampposteffect in forum New To Java
    Replies: 12
    Last Post: 04-21-2011, 07:10 PM
  2. Replies: 4
    Last Post: 01-31-2011, 08:20 PM
  3. Array index out of bounds error
    By blackstyle18 in forum New To Java
    Replies: 3
    Last Post: 12-28-2010, 02:37 AM
  4. array going out of bounds?
    By jabo in forum New To Java
    Replies: 9
    Last Post: 04-02-2010, 10:08 AM
  5. Inconsistent out-of-bounds error
    By geforce2000 in forum AWT / Swing
    Replies: 7
    Last Post: 12-23-2009, 08:27 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
  •