0
public static void randomNumberGame() {
    Scanner input = new Scanner(System.in);
    Random r = new Random();
    int x; 
    int y; 
    int random;
    int total = 0;
    int[][] board = new int[5][5];

    for (int i = 0; i < 5; i++) {
        System.out.print(" " + (i + 1) + " ");
    }
    System.out.println("");

    for (int i = 0; i < board.length; i++) {
        System.out.print(i + 1);
        for (int j = 0; j < board[0].length; j++) {
            board[i][j] = 0;
            System.out.print("[ ]");
        }
        System.out.println("");
    }

    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[0].length; j++) {
            random = r.nextInt(10) + 1;
            if (board[i][j] == 0) {
                board[i][j] = random;
            }

        }
    }

    for (int i = 0; i < 3; i++) {
        System.out.println("Please choose x coordinate for spot " + (i + 1));
        x = input.nextInt();
        System.out.println("Please choose y coordinate for spot " + (i + 1));
        y = input.nextInt(); 

        total += board[x][y];
            System.out.println(total);            

    }


}

The output is as follows:

1 2 3 4 5

1[ ][ ][ ][ ][ ]

2[ ][ ][ ][ ][ ]

3[ ][ ][ ][ ][ ]

4[ ][ ][ ][ ][ ]

5[ ][ ][ ][ ][ ]

Please choose x coordinate for spot 1: 5

Please choose y coordinate for spot 1: 5

Exception Stack Trace -

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at chapter.pkg9.and.pkg10.test.Chapter9And10Test.randomNumberGame(Chapter9And10Test.java:117)
at chapter.pkg9.and.pkg10.test.Chapter9And10Test.main(Chapter9And10Test.java:22) C:\Users\Lance\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)

Line 117 is the one containing this total += board[x][y];

Bond - Java Bond
  • 3,972
  • 6
  • 36
  • 59
Lance
  • 47
  • 3

1 Answers1

2

Because 5 (the value you've entered for both x and y) is not a valid x or y value. The valid indexes for your arrays are 0 to 4, inclusive. (As in all of your loops setting things up.) Either have your user enter a value from 0-4 (inclusive), or allow them to enter 1-5 (inclusive) and remove one from x and y prior to using them.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • So i would have to do: System.out.println("Please choose x coordinate for spot " + (i + 1)); x = input.nextInt(); System.out.println("Please choose y coordinate for spot " + (i + 1)); y = input.nextInt(); – Lance May 09 '17 at 06:04
  • Something like `total += board[x-1][y-1];` would be fine. – dat3450 May 09 '17 at 06:04
  • Sorry, I was trying to fix formatting, I don't know how to format code in the comments – Lance May 09 '17 at 06:05
  • @Lance EIther make your user enter 0-4, or make them enter 1-5 and subtract 1 from `x` and `y` prior to using them. – T.J. Crowder May 09 '17 at 06:05
  • @ T.J. I understand now thanks – Lance May 09 '17 at 06:06
  • 1
    You forgot to check if values of `x` and `y` are inbound of your 2d array. Remember: `int[][] board = new int[5][5]` bound is `bound[0-4][0-4]`!! – Binyamin Regev May 09 '17 at 06:06