Jump to content

Hey guys; whenever I run this program:

package minesweeper;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]);                  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);           for (int i = 1; i <= M; i++) {         for (int j = 1; j <= N; j++)            if (bombs[i][j]) System.out.print("x ");            else             System.out.print(". ");         System.out.println();      }           int[][] sol = new int[M+2][N+2];      for (int i = 1; i <= M; i++)         for (int j = 1; j <= N; j++)              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]++;           System.out.println();      for (int i = 1; i <= M; i++) {         for (int j = 1; j <= N; j++)            if (bombs[i][j]) System.out.print("x ");            else             System.out.print(sol[i][j] + " ");         System.out.println();      }   }}

I get the error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

at minesweeper.MineSweeper.main(MineSweeper.java:6)
 
 
Does anyone know what the issue might be?

 

Link to comment
https://linustechtips.com/topic/102040-minesweeper-java-noob-help/
Share on other sites

Link to post
Share on other sites

Well I have a suspicion of where the "code" is going wrong

      int M = Integer.parseInt(args[0]);      int N = Integer.parseInt(args[1]);      double p = Double.parseDouble(args[2]);

the args is being used without even attempting to see if args has length 3.

if(args.length < 3) {    System.out.println("Error usage must be minesweeper [M] [N] [Probability]");    return;}

Before you use an item in an array (which effectively is user defined) you need to check that the user has inputted that data, because in this case you are likely just running the program like minesweeper.java instead of "minesweeper.java 10 10 0.5".  If you want user input it is actually wise to just use System.in to get the user input

0b10111010 10101101 11110000 00001101

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×