java.lang.ArrayIndexOutOfBoundsException: 0; no se por que

Iniciado por juligarc10, 9 Marzo 2019, 13:17 PM

0 Miembros y 2 Visitantes están viendo este tema.

juligarc10

Hola amigos, no entiendo por que me da este error en el codigo. Una ayudita por favor.

package IU;



/**
*
* @author AEDI
*/
public class Domino {
        private static int numJugadores=0;
        private static String [] Jugadores=new String [numJugadores];
   
    public static void inicioPartida() {
numJugadores=ES.pideNumero("Cuantos jugadores participaran en la partida? ");
                for (int i=0;i<numJugadores;i++){
                    Jugadores[i]=ES.pideCadena("Introduzca el nombre del jugador numero "+ i+" " );
                }
         

}
   
}

rub'n

#1
Cita de: juligarc10 en  9 Marzo 2019, 13:17 PM
Hola amigos, no entiendo por que me da este error en el codigo. Una ayudita por favor.

package IU;



/**
*
* @author AEDI
*/
public class Domino {
       private static int numJugadores=0;
       private static String [] Jugadores=new String [numJugadores];
   
   public static void inicioPartida() {
numJugadores=ES.pideNumero("Cuantos jugadores participaran en la partida? ");
               for (int i=0;i<numJugadores;i++){
                   Jugadores[i]=ES.pideCadena("Introduzca el nombre del jugador numero "+ i+" " );
               }
       

}
   
}


Que tal usa GeSHi




jugadores lo seteas a cero, por eso no puede llenar mas posiciones, pide el numero de jugadores y con eso das tamaño a el

Código (java) [Seleccionar]
public class SimpleArray {

   private static int numJugadores=0;
   private static String [] jugadores;
   private static final Scanner SCANNER = new Scanner(System.in);

   public SimpleArray() {
       inicioPartida();
   }

   public static void inicioPartida() {

       System.out.println("\"Cuantos jugadores participaran en la partida ?");
       numJugadores = SCANNER.nextInt();

       jugadores = new String[numJugadores];

       for (int i = 0; i < jugadores.length; i++) {
           System.out.println("Introduzca el nombre del jugador numero " + (i++) + " ");
           jugadores[i] = SCANNER.next();
       }
   }

   public static void main(String ...magfaf) {
       new SimpleArray();
   }

}


PD... constante en mayúsculas SCANNER


rubn0x52.com KNOWLEDGE  SHOULD BE FREE!!!
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen