Reputation: 69
I'm a newbie to java
. Can anyone pls help me with resolving the error arrayindexoutofboundsexception
.
public class Minesweeper {
public static void main(String[] args) {
int M = Integer.parseInt(args[0]);
int N = Integer.parseInt(args[1]);
double p = Double.parseDouble(args[2]);
// game grid is [1..M][1..N], border is used to handle boundary cases
boolean[][] bombs = new boolean[M+2][N+2];
for (int i = 1; i <= M; i++)
for (int j = 1; j <= N; j++)
bombs[i][j] = (Math.random() < p);
// print game
for (int i = 1; i <= M; i++) {
for (int j = 1; j <= N; j++)
if (bombs[i][j]) System.out.print("* ");
else System.out.print(". ");
System.out.println();
}
// sol[i][j] = # bombs adjacent to cell (i, j)
int[][] sol = new int[M+2][N+2];
for (int i = 1; i <= M; i++)
for (int j = 1; j <= N; j++)
// (ii, jj) indexes neighboring cells
for (int ii = i - 1; ii <= i + 1; ii++)
for (int jj = j - 1; jj <= j + 1; jj++)
if (bombs[ii][jj]) sol[i][j]++;
// print solution
System.out.println();
for (int i = 1; i <= M; i++) {
for (int j = 1; j <= N; j++)
if (bombs[i][j]) System.out.print("* ");
else System.out.print(sol[i][j] + " ");
System.out.println();
}
}
}
and here is the exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Minesweeper.main(Minesweeper.java:5)
Upvotes: 4
Views: 41800
Reputation: 11612
(SOLUTION #1)
You have used the command line arguments as below, args[0], args1 and args[3] means your class need 3 parameters in order to run correctly however you've provided no parameters thus,
// int M = Integer.parseInt(args[0]);
// int N = Integer.parseInt(args[1]);
// double p = Double.parseDouble(args[2]);
int M = 2;
int N = 3;
double p = 3.99;
Then your code output will be;
* * *
* * *
* * *
* * *
Your code needs 3 parameters and what I've done above is just assigning those parameters with assignment instead of using the values of args[] array.
(SOLUTION #2)
Instead of changing your code, you can pass 3 command-line arguments which stands for int M, N and double p. To do this in eclipse ide;
Now you won't get "ArrayIndexOutOfBoundsException" again, with values 2 4 0.9, the output will be;
* * . *
* . * *
* * 4 *
* 4 * *
(EXPLANATION)
There are two problem in your code;
You have the ArrayIndexOutOfBoundsException exception which means your code tries to reach an array value with an exceeding index.
You are using command line arguments but you are not entering any arguments as a parameter. Your main method needs 3 parameters in order to run correctly but with no parameters. args[0]
, args[1]
and args[2]
means there is an array names with args[] and you are trying to reach 0th, 1st and 2nd elements of the args[] array. However, if you provide no command-line parameter, you are trying to reach a null value and thus you get;
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at com.stack1.Minesweeper.main(Minesweeper.java:8)
To make it clear, first I will explain what is ArrayIndexOutOfBoundsException;
ArrayIndexOutOfBoundsException error occurs when a statement in your code tries to reach an array index where the index is greater than the length of the array. Let me explain this;
Assume that you have 2 cars. The color of your first car is red, and the color of your second car is blue. If I ask you that "what's your second car's color" , you will return me the answer as blue. But what is I ask "what's your fifth car's color?" what you will say is "I don't own a fifth car".
"ArrayIndexOutOfBoundsException" error is the same, in this case, you just return an "ArrayIndexOutOfBoundsException" error because the index 5 is greater than the maximum count of your car indexes, which actually the length of the car array.
String car[] = new String[2];
car[0] = "blue";
car[1] = "red";
To make it clear, let's run the code below;
public class CarColorExample
{
public static void main(String[] args)
{
String[] carArray = new String[2];
carArray[0] = "blue";
carArray[1] = "red";
System.out.println("1st car color value: " + carArray[0]);
System.out.println("2nd car color value: " + carArray[1]);
System.out.println("3rd car color value: " + carArray[2]);
}
}
If you try and run the code above, you will get the exception as below;
1st car color value: blue
2nd car color value: red
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at com.stack1.CarColorExample.main(CarColorExample.java:15)
There is the statement "java.lang.ArrayIndexOutOfBoundsException: 2" which actually points to the exceeding index tells you that the carArray does not have a second index.
index: 0 --> "blue"
index: 1 --> "red"
index: 2 --> ????
As a second example about the ArrayIndexOutOfBoundsException error, just check out the code below and run it;
public static void main(String[] args)
{
int[] intArray = new int[3];
intArray[0] = 25;
intArray[1] = 36;
intArray[2] = 99;
//Will work with no error
for(int i = 0; i < intArray.length; i++)
System.out.println("index[" + i + "], value:" + intArray[i]);
// Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
// at com.stack1.ErrorExample2.main(ErrorExample2.java:18)
//
// "ArrayIndexOutOfBoundsException" error will occur for index: 3
// The length of the array is 2
// index: 0 --> 25
// index: 1 --> 36
// index: 2 --> 99
// There is no third index, That's all
for(int i = 0; i < intArray.length + 1; i++)
System.out.println("index[" + i + "], value:" + intArray[i]);
}
}
Same problem in here, index '3', exceeds the maximum index of the array which is exactly stored in "intArray.length"
Upvotes: 4
Reputation: 4805
You have to check if there is two args or not, with a code like this:
if(args.length > 2)
Also I think it is better to change lines like this :
for (int i = 1; i <= M; i++) {
for (int j = 1; j <= N; j++)
to this:
for (int i = 0; i <M; i++) {
for (int j = 0; j < N; j++)
Upvotes: 3
Reputation: 32478
You have to check the length of the array args
before accessing an element. Since you have to access 2nd element in the array, the lengh should be atleast 3. You have to check that like below
if(args.length > 2) {
//wrap all code here
}
Upvotes: 5
Reputation: 37083
Two things probably as it says index 0 (see first case for the same):
You are not passing arguments to your main class.
Array index always start from 0 and not 1. So you might need to change either:
Reason you are getting ArrayIndexOutOfBoundException is lets say you have 2 elements in an array. It will be as below:
a[0] = 1;
a[1] = 2;
And when you are looping using i = 1; i<=2 you are accessing:
a[1] - which is perfect
a[2] - which was not expected?
Upvotes: 9