简体   繁体   中英

Getting a Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException error, and am stuck trying to figure out a solution

I'm writing a bit of code for a school project. However, I'm getting a:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 5 at TicTacToe.main(TicTacToe.java:25)

(This is the board[3-w1[i][0]][(w1[i][1])]='O'; bit)

This is a sample input:

5 (size of array)

1 0 0 (the number of black pieces to be placed on the board, followed by B pairs and the row and column of B black pieces)

1 4 4 (the number of white pieces to be placed on the board, followed by W pairs and the row and column of W white pieces)

4 0 0 1 1 1 1 2 2 2 2 3 1 3 1 4 0 (the number of moves, followed by 4 integers: Row 1 Column 1 Row 2 Column 2 which is the row and column of the start and end position of a move. Ie, the piece at R1 C1 is moved to R2 C2)

import java.util.Scanner;

public class TicTacToe {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
  
    // Write your code here
    int i,j;
    int n=in.nextInt();
    char board[][]=new char[n][n];
    int b=in.nextInt();

    int b1[][]=new int[b][2];
    for(i=0;i<b;i++){
        b1[i][0]=in.nextInt();
        b1[i][1]=in.nextInt();
        board[3-b1[i][0]][b1[i][1]]='*';
    }
    
    int w=in.nextInt();
    int w1[][]=new int[w][2];
    for(i=0;i<w;i++){
        w1[i][0]=in.nextInt();
        w1[i][1]=in.nextInt();
        board[3-w1[i][0]][(w1[i][1])]='O';
    }

    for(i=0;i<n;i++){
      for(j=0;j<n;j++){
        if(board[i][j]!='*' && board[i][j]!='O')
          board[i][j]='.';
      }
    }
    int moves=in.nextInt();
    char tag;
    int m[][]=new int[moves*2][2];
    for(i=0;i<(moves*2);i++){
      m[i][0]=in.nextInt();
       m[i][1]=in.nextInt();
    }

    for(i=0;i<(moves*2);i++){
      tag=' ';
      tag=board[3-m[i][0]][m[i][1]];
      board[3-m[i][0]][m[i][1]]='.';
      i++;
      board[3-m[i][0]][m[i][1]]=tag;
    }
    for(i=0;i<n;i++){
      for(j=0;j<n;j++){
        System.out.print(board[i][j]+"");
      }
      System.out.println();
    }
  }
}

Is there a way to fix the error?

Debug this code.

Debugging basically means: Go through the code by 'hand', and figure out in your head what it should be doing. Use pen and paper if you have to.

Then, run the actual code and observe what it actually does. Normally one would use a debugger, but if that feels like a daunting task somehow, hey, there's always the option of litterring a ton of System.out.println statements around. One way or another, you must get insights into what the system is doing.

Then, where the program runs differently than what you thought would happen, guess what? You found one! There may be more, but that's definitely a problem. Investigate, fix, rerun, keep going until the app works.

So, let's get to this specific case: That error means that you are writing arr[i] where arr is an array of size 5, and i is -1, and java has no idea what -1 might mean. Given that it's the board[3-w1[i][0]][(w1[i][1])]='O'; line, that means i is -1, or, w1[i][0] is 4 (resulting in 3-4 , which is -1 ), or w1[i][1] is -1. That's a lot of options which is exactly why you need to debug: Clearly you think this code is such that none of these things could ever be -1, yet, they are. So, print all that stuff. If you can't figure out why something you print is what it is, then find out where that variable is set and print every time you tweak it. Eventually you'll figure out where the problem lies and fix it.

The alternative is that you order a Crystal Ball from amazing voodoo magic coders Ltd, which is how professional coders work: They just gaze into it and divine where errors are, writing perfect code, which is why software you use daily is 100% bug free. Alas. If only. No such thing as crystal balls, I'm afraid. That's what we all do - debug our code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM