Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › java.lang.ArrayIndexOutOfBoundsException
New Posts  All Forums:Forum Nav:

java.lang.ArrayIndexOutOfBoundsException

post #1 of 3
Thread Starter 
Help...

This is the output for the piece of code below. I get that error if I try to use getMove more than once. Can anybody figure out why? I put checks inside the getMove method in order to tell exactly where the problem is. Anybody have any input on this?

Everything else except for the solveMaze method works great. Solvemaze calls getMove, then I displayed the matrix to see if it worked, then it calls getMove again and all goes to hell. I won't be here until the morning, so I can't answer any questions. I really need help with this. Thanks

Code:
Lab 29a 

Enter random starting seed === >> 5

RANDOM MAZE DISPLAY

O X X X X X X X X X X X 
X O X X O X O X O X X X 
X O O X X O O X X O O X 
X O X X X O X X X X O X 
X O O O X X O X X O X X 
X O X X O X X O O X X X 
X X O X X X X X O O O X 
X X O X X X X X O X X X 
X O O O O O O O O O X X 
X X O O X O O X X X O X 
X O O O X X O O X X O X 
X X X X X X X X X X X . 


Press <Enter> to continue ===>> 

>>>>> WORKING .... SOLVING MAZE <<<<<

Check 1
Check 2

RANDOM MAZE DISPLAY

O X X X X X X X X X X X 
X O X X O X O X O X X X 
X O O X X O O X X O O X 
X O X X X O X X X X O X 
X O O O X X O X X O X X 
X O X X O X X O O X X X 
X X O X X X X X O O O X 
X X O X X X X X O X X X 
X O O O O O O O O O X X 
X X O O X O O X X X O X 
X O O O X X O O X X . X 
X X X X X X X X X X X . 


Press <Enter> to continue ===>> 
Check 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
    at Maze.getMove(Lab29a0.java:155)
    at Maze.solveMaze(Lab29a0.java:114)
    at Lab29a0.main(Lab29a0.java:19)

Process completed.


Code:
//Lab29ast.java
//this is the student version of the Lab 29a assignment. Complete this file as is.

import java.io.*;
import java.util.*;
import java.util.ArrayList;

public class Lab29a0 
{
    public static void main(String[] args) throws IOException
    {
    System.out.println("nLab 29a n");
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter random starting seed === >> ");
    int seed = Integer.parseInt(input.readLine());
    
    Maze maze = new Maze(seed);
    maze.displayMaze();
    maze.solveMaze();
    maze.displayMaze();
    maze.mazeSolution();
    }
}

class Coord //Coord is a class that stores a single maze location.
{
private int rPos;
private int cPos;
public Coord (int r, int c) 
{
rPos = r; 
cPos = c;
}
public boolean isFree() 
{
return (rPos == 0 && cPos == 0);
}
public void setPos(int r, int c) 
{
rPos += r; 
cPos += c;
}
public int getrPos() 
{
return rPos;
}
public int getcPos() 
{
return cPos;
}
}

class Maze
{
private char mat[][]; //2d character array that stores the maze display
private Coord currentMove; //object that stores current maze position
private MyStack visitStack; //stack that stores locations that have been visited

public Maze(int seed)
/* constructor which generates the random maze, random starting location
and initializes Maze class values. If the random value equals 0 the maze 
stores an 'X' otherwise it stores and 'O' in the maze.*/
{
Random random = new Random(seed);
int startRow, startCol;
mat = new char[12][12];
for(int r = 0; r < 12; r ++)
{
for (int c = 0; c < 12; c ++)
{
if (r == 0 || c == 0 || r == 11 || c == 11)
mat[r][c] = 'X';
else
{
int rndInt = random.nextInt(2);
if (rndInt == 0)
mat[r][c] = 'X';
else
mat[r][c] = 'O';
}
}
}
mat[0][0] = 'O';
startRow = random.nextInt(12);
startCol = 11;
mat[startRow][startCol] = '.';
visitStack = new MyStack();
currentMove = new Coord(startRow, startCol);
visitStack.push(currentMove);
}

void displayMaze() throws IOException
// displays the current maze configuration
{
System.out.println("nRANDOM MAZE DISPLAYn");
for (int r = 0; r < 12; r++)
{
for (int c = 0; c < 12; c++)
System.out.print(mat[r][c] + " ");
System.out.println();
}
System.out.println();
pause();
}

public void solveMaze() throws IOException
// This method solves the maze with private helper method <getMove>.
// A loop is needed to repeat getting new moves until either a maze solution
// is found or it is determined that there is no way out of the maze.
{
System.out.println("n>>>>> WORKING .... SOLVING MAZE <<<<<n");
getMove();
displayMaze();
getMove();
}

public void mazeSolution()
//Short method to display the result of the maze solution
{
if (currentMove.isFree())
System.out.println("nTHE MAZE HAS A SOLUTION.n");
else
System.out.println("nTHE MAZE HAS NO SOLUTION.n");
}

private boolean inBounds(int r, int c)
//This method determines if a coordinate position is inbounds or not
{
boolean booly = false;

if (r >= 0 && r <= 11 && c >= 0 && c <= 11)
booly = true;
else
booly = false;

return booly;

}

private boolean getMove() throws IOException
/*This method checks eight possible positions in a counter clockwise manner
 starting with the (-1,0) position. If a position is found the method returns
 true and the currentMove coordinates are altered to the new position 
 */
 {
 boolean booly = false;
int r, c, a, b;
a = currentMove.getrPos();
b = currentMove.getcPos();
r = currentMove.getrPos();
c = currentMove.getcPos();
r -= 1;
c += 0;
System.out.println("Check 1");
 if((mat[r][c] == 'O') && (mat[r][c] != 'X') && (inBounds(r,c) == true))
 {
 System.out.println("Check 1");
 mat[r][c] = '.';
 currentMove.setPos(r,c);
 int x = (r + 1)*(c + 1);
 visitStack.push(x);
 booly = true;
 return booly;
 }
 else
 {
 booly = false;
 }
 
r = a;
 c = b;
 r -= 1;
c -= 1;
System.out.println("Check 2");
 if((mat[r][c] == 'O') && (mat[r][c] != 'X') && (inBounds(r,c) == true))
 {
 mat[r][c] = '.';
 currentMove.setPos(r,c);
 int x = (r + 1)*(c + 1);
 visitStack.push(x);
 booly = true;
 return booly;
 }
 else
 {

 booly = false;
 }
 r = a;
 c = b;
 r -= 0;
c -= 1;
 System.out.println("Check 3");
 if((mat[r][c] == 'O') && (mat[r][c] != 'X') && (inBounds(r,c) == true))
 {
 mat[r][c] = '.';
 currentMove.setPos(r,c);
 int x = (r + 1)*(c + 1);
 visitStack.push(x);
 booly = true;
 return booly;
 }
 else
 {
 booly = false;
 }
 r = a;
 c = b;
 r += 1;
c -= 1;
 System.out.println("Check 4");
 if((mat[r][c] == 'O') && (mat[r][c] != 'X') && (inBounds(r,c) == true))
 {
 mat[r][c] = '.';
 currentMove.setPos(r,c);
 int x = (r + 1)*(c + 1);
 visitStack.push(x);
 booly = true;
 return booly;
 }
 else
 {
 booly = false;
 }
 r = a;
 c = b;
 r += 1;
c -= 0;
 System.out.println("Check 5");
 if((mat[r][c] == 'O') && (mat[r][c] != 'X') && (inBounds(r,c) == true))
 {
 mat[r][c] = '.';
 currentMove.setPos(r,c);
 int x = (r + 1)*(c + 1);
 visitStack.push(x);
 booly = true;
 return booly;
 }
 else
 {
 booly = false;
 }
 r = a;
 c = b;
 r += 1;
c += 1;
 System.out.println("Check 6");
 if((mat[r][c] == 'O') && (mat[r][c] != 'X') && (inBounds(r,c) == true))
 {
 mat[r][c] = '.';
 currentMove.setPos(r,c);
 int x = (r + 1)*(c + 1);
 visitStack.push(x);
 booly = true;
 return booly;
 }
 else
 {
 booly = false;
 }
  r = a;
 c = b;
 r += 0;
c += 1;
System.out.println("Check 7");
 if((mat[r][c] == 'O') && (mat[r][c] != 'X') && (inBounds(r,c) == true))
 {
 mat[r][c] = '.';
 currentMove.setPos(r,c);
 int x = (r + 1)*(c + 1);
 visitStack.push(x);
 booly = true;
 return booly;
 }
 else
 {

 booly = false;
 }
r = a;
 c = b;
 r -= 1;
c += 1;
System.out.println("Check 8");
 if((mat[r][c] == 'O') && (mat[r][c] != 'X') && (inBounds(r,c) == true))
 {
 mat[r][c] = '.';
 currentMove.setPos(r,c);
 int x = (r + 1)*(c + 1);
 visitStack.push(x);
 booly = true;
 return booly;
 }
 else
 {
 booly = false;
 }
 return booly;
 
 }
 
 private void pause() throws IOException
 {
 BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
 String dummy;
 System.out.print("nPress <Enter> to continue ===>> ");
 dummy = input.readLine();
 }
}

class MyStack<Object>
{

private ArrayList<Object> data;// stores stack data
private int top;// keeps index of the stack top

public MyStack()
// Initializes an empty array object with references of private variable data objects.
{
data = new ArrayList<Object>();
top = -1;
}

public boolean isEmpty()
// Returns true if data is empty, false otherwise
{
return top == -1;
}

public void push (Object x)
// Adds variable x to the top of the stack
{
data.add(x);
top++;
}

public Object pop()
// Returns and removes the top element from the stack
{
int temp = top;
top--;
return data.remove(temp);
}

public Object peek()
// Returns the top element from the stack without removal
{
return data.get(top);
}

}
Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 Gigabyte GA-EP45-UD3P Sapphire 5870 Cypress XT EF6 2GB 4GB (2x2GB) G.SKILL 1066 
Hard DriveOptical DriveOSMonitor
32GB Corsair SSD + 2x 320 + 1x 250 Lightscribe drive Windows 7 Ultimate Hanns-G HG-216DPO 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard OCZ GameXStream 850W HAF 932 Logitech G5 Laser Gaming Mouse 
Mouse Pad
Allsop 
  hide details  
Reply
Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 Gigabyte GA-EP45-UD3P Sapphire 5870 Cypress XT EF6 2GB 4GB (2x2GB) G.SKILL 1066 
Hard DriveOptical DriveOSMonitor
32GB Corsair SSD + 2x 320 + 1x 250 Lightscribe drive Windows 7 Ultimate Hanns-G HG-216DPO 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard OCZ GameXStream 850W HAF 932 Logitech G5 Laser Gaming Mouse 
Mouse Pad
Allsop 
  hide details  
Reply
post #2 of 3
Just from the error, an ArrayOutOfBoundException is thrown when you try and access an element of an array that is outside the range of possibles indices. In other words, if your array has n elements, and you try and index a value that is < 0 or > n-1, you'll get that exception thrown. Take a close look at your r and c index. I'd put a watch on the variable (if you're using an advanced IDE) so you can see exactly how the r and c indices are changed.
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
post #3 of 3
Thread Starter 
I fixed it today. The currentmove class was improperly written. rPos shouldn't equal += r. It should just equal r. Same goes for cPos.
Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 Gigabyte GA-EP45-UD3P Sapphire 5870 Cypress XT EF6 2GB 4GB (2x2GB) G.SKILL 1066 
Hard DriveOptical DriveOSMonitor
32GB Corsair SSD + 2x 320 + 1x 250 Lightscribe drive Windows 7 Ultimate Hanns-G HG-216DPO 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard OCZ GameXStream 850W HAF 932 Logitech G5 Laser Gaming Mouse 
Mouse Pad
Allsop 
  hide details  
Reply
Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 Gigabyte GA-EP45-UD3P Sapphire 5870 Cypress XT EF6 2GB 4GB (2x2GB) G.SKILL 1066 
Hard DriveOptical DriveOSMonitor
32GB Corsair SSD + 2x 320 + 1x 250 Lightscribe drive Windows 7 Ultimate Hanns-G HG-216DPO 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard OCZ GameXStream 850W HAF 932 Logitech G5 Laser Gaming Mouse 
Mouse Pad
Allsop 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › java.lang.ArrayIndexOutOfBoundsException