# Array Index Out Of Bounds Exception

Hey,

I’m developing a Mine Sweeper game through Java.

I’m having an issue where sometimes the two integers given by the user throws out an exception of out of bounds. I’m using a array from 0 - 19 position

I thought if I used:

``````
for (int m=-1; m<1; m++) {
for (int n=-1; n<=1; n++) {
if ((m!=0) || (n!=0)) {

``````

That it would keep the positions within the array. Does anyone know how to fix this issue?

I’m also having a second issue. I’m trying to hide the mines so the user who is playing the game can’t see the mines. This is what I have now

``````
Random rand = new Random();
int mineChecker = 0;

while (true)
{
int mine1 = rand.nextInt(19);
int mine2 = rand.nextInt(19);
Table[mine1][mine2] = '*';
mineChecker++;
if (mineChecker == BOMBS) //BOMBS is an int I defined earlier in my program
break;
}

``````

Now this works fine but it displays the in my array so it defeats the purpose of the game.

I tried doing

``````
Random rand = new Random();
int mineChecker = 0;

while (true)
{
int mine1 = rand.nextInt(19);
int mine2 = rand.nextInt(19);
Table[mine1][mine2] = '*';
mineChecker++;
if (Table[mine1][mine2] == '*')
Table[mine1][mine2] = ' ';
if (mineChecker == BOMBS)
break;
}

``````

But then that just clears all the mines :-\

Can anyone lend a hand? thanks!

For creating your mines try the following code:

``````

// Class level variable maybe?
Random rand = new Random();

public void init(int rowCount, int columnCount, int totalMines) {

Object[][] tableArray = new Object[rowCount][columnCount];

for(int i = 0; i < totalMines; i++}
{
setRandomMine(tableArray);
}
}

public void setRandomMine(Array[][] tableArray) {

int rowCount = tableArray.length;

int selectedRow = random.nextInt(rowCount - 1);

int columnCount = tableArray[selectedRow].length;

int selectedColumn = random.nextInt(columnCount);

if(tableArray[selectedRow][selectedColumn] != "*") {

tableArray[selectedRow][selectedColumn] = "*";

} else {

setRandomMine(tableArray);
}
}

``````

Your current code sets a bomb, checks to see if the same exact location in the array is a bomb and then sets the value to blank.

Best of Luck.

hello friend

if it is not yet solved do contact me i can help you with this issue.

regards
mohneesh

Is this what I think it is? In any case, if you have any advise or solution, you should post publicly. Sounds like you want to make \$\$\$ on a public site but I could be wrong.