Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 14th, 2007, 5:23 PM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 344
Rep Power: 4 cwl157 is on a distinguished road
arrayIndexOutOfBounds exception

I need to make a 4 methods that take a 2d integer array thats 3x3 and look for a 0 and swap it with the number left up right or down from it. I have to make 4 of them for up down left and right. I have the left and up ones working but for some reason i get an arrayIndexOutOfBounds exception when i try it for the right and down ones. The only difference i can see is a +1 instead of a -1 for the position in the 2d array. Here is the method for moving left and for moving right and the printNode() method. Can someone see where the mistake is cause i cant? Thanks.

moveLeft() method
 // move it left
   public void moveLeft(int[][] state)
   {
      for (int row = 0; row < 3; row++)
      {
         for (int col = 0; col < 3; col++)
         {
             if (state[row][col] == 0)
             {
                 state[row][col] = state[row][col-1];
                 state[row][col-1] = 0;
                 printNode();
             } // end if
         } // end for
      } // end for
   } // end moveLeft

moveRight() method
 // move it right
   public void moveRight(int[][] state)
   {
      for (int row = 0; row < 3; row++)
      {
         for (int col = 0; col < 3; col++)
         {
             if (state[row][col] == 0)
             {
                 state[row][col] = state[row][col+1];
                 state[row][col+1] = 0;
                 System.out.println("the row is: " + row);
                 System.out.println("the col is: " + col);
                 printNode();
             } // end if
         } // end for
      } // end for
   } // end moveRight

printNode() method
// print the current state
   public void printNode()
   {
      for(int row = 0; row < 3; row++)
      {
        for(int col = 0; col < 3; col++)
        {
          System.out.print(state[row][col] + "   ");
        } // end for
        
        System.out.println();
      } // end for
    } // end printNode

The error happens right before printNode() is called but it still prints the 2d array with the swap made. Also, i have 4 other methods that check to make sure the 0 is in a legal spot to where there is room to the left or right for it to move to. For testing i have the 0 in the middle so it shouldnt be a problem. And you'll notice in the moveRight() method i print the row and col and it matches up.
cwl157 is offline   Reply With Quote
Old Sep 14th, 2007, 5:46 PM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Assuming the array (state) is 3 by 3, you should not access elements with indices outside the range [0,2]. Which means that accessing state[i][j] implies a condition that both i and j are in the range [0,2].

In your MoveLeft() method, col-1 will be -1 if col is 0, which is out of the range [0,2].

In your MoveRight() method, col+1 will be 3 if col is 2, which is out of the range [0,2].
grumpy is offline   Reply With Quote
Old Sep 14th, 2007, 5:58 PM   #3
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 344
Rep Power: 4 cwl157 is on a distinguished road
but the only time i should get the error for move left is if the 0 is say positioned like this:

[1, 2, 3
0, 4, 5
6, 7, 8]

if it was like this i cant move left because i would be replacing the spot at -1 which isnt allowed same for the right if its set up like this:

[1, 2, 3
4, 5, 0
6, 7, 8]

then i shouldnt be able to move right because i would be accessing 3 which like you said is out of range, but my test is like this

[1, 2, 3
4, 0, 5
6, 7, 8]

so this should not be a problem for replacing 1 to the right of 0 with 0 because it should still be in range but for some reason its saying its not.
cwl157 is offline   Reply With Quote
Old Sep 14th, 2007, 6:18 PM   #4
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 344
Rep Power: 4 cwl157 is on a distinguished road
alright i figured it out. It is going through the 2d array from top left to bottom right, so therefore if i move it either left or up then its no problem cause its past those but if i move it right or down then it hits the 0 again as it goes through the loop. This helped answer a question that i had though and that was where do i want to put the checks to make sure it can be moved and the answer is right before i do it inside the moveRight() method. But now i have another problem because i want it to move one at a time so its like right after i move it i have to break out of both loops, or maybe just end the method entirely?

EDIT: Yep i just ended the method right after i make the first swap. Since the only purpose of the method is to move the 0 once in the specified direction, once i move it once theres no reason to let the loop go through the rest of the 2d array, so i just return it.

Last edited by cwl157; Sep 14th, 2007 at 6:39 PM.
cwl157 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
exception handling class rwm C++ 5 Apr 10th, 2007 5:17 AM
exception handling l2u C++ 2 Mar 3rd, 2007 5:47 PM
coldfire assembly - rts causes Illegal Instruction Exception mika Assembly 4 Jun 18th, 2006 3:35 PM
Could some please explain classes to me... TCStyle C++ 10 Feb 20th, 2006 3:51 PM
AhhH!!! I can't find anything on this exception... stakeknife ASP 2 Sep 26th, 2005 7:48 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:27 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC