[Solved] Catching ArrayIndexOutOfBoundsExc


An ArrayIndexOutOfBoundsException should generally not be handled as it is considered as a programming error. However, if you want to do this.. just write it the more natural way :

try {
    for(row=row-1;row>=0;row--) {
       if(tablero[col1][row]==".") {
           tablero[col1][row]="X";
           break;
       }
    }
} catch (ArrayIndexOutOfBoundsException ex) {
    // do something
}

However, from your edited message, it would be better to do something like this :

public static int preguntarHastaAcertar (Scanner in, String mensaje, int nrows){
     System.out.print(mensaje);
     String linea = in.nextLine();
     try {
         int value = Integer.parseInt(linea) - 1;
         if (value >= 0 && value < nrows)
             return value; 
         else {
             System.out.print(linea + " no es una columna correcta!");
             return preguntarHastaAcertar(in,mensaje,nrows);
         }
     } catch (NumberFormatException e){
         System.out.print(linea + " no es un numero!");
         return preguntarHastaAcertar(in,mensaje,nrows);
     }            
}

2

solved Catching ArrayIndexOutOfBoundsExc