I can not understand why I get this error ArrayIndexOutOfBoundsException: 6

1

I'm getting an Exception in thread "main"

  

java.lang.ArrayIndexOutOfBoundsException: 6 exactly at this point.

     

at primeraclase.PrimeraClase.main (Seat 14H FirstClass.java:81

It is supposed that it should reach up to 60 because the array is 6 x 10.

Does anyone give me a clue as to what may be happening?

package primeraclase;
import java.util.Scanner;
public class PrimeraClase {


public static void main(String[] args) {

    //EL CODIGO SIGUIENTE ES PARA CREAR LOS ARRAYS QUE CONTIEIEN LOS ASIENTOS 
    //AQUÍ ESTAN LAS SENTIENCIAS PARA LA PRIMERA CLASE
    String[][] primeraClase = new String [6][5];       
    String filasPrimeraClase = "1";
    String asiento;
    char columnaPrimeraClase = 65;
    String letraColumna = Character.toString(columnaPrimeraClase);
    int limiteYprimeraClase = 5;
    int limiteXprimeraClase = 4;
    int contadorPrimeraClase = 0;
    int xp = 0;
    int yp = 0;
    int xAleatorio = 0;       
    int xVentana = 0;
    int xPasillo = 0;

    //AQUI SE DECLARAN TODAS LAS VARIABLES DE LA CLASE TURISTA

    String[][] claseTurista = new String [6][10];       
    String filasClaseTurista = "1";
    String asientoC;
    int limiteYClaseT = 5;
    int limiteXClaseT = 9;
    int contadorClaseT = 0;
    int xpT = 0;
    int ypT = 0;
    int xAleatorioClaseT = 0;       
    int xVentanaClaseT = 0;
    int xPasilloClaseT = 0;
    char columnaClaseT = 70;
    String letraColumnaT = Character.toString(columnaClaseT);


    //AQUI ESTAN LAS VAARIABLES QUE SE COMPARTEN  EN AMBAS CLASES
    int yAleatorio = 1;
    int yVentana = 0;
    int yPasillo = 2;
    int limiteYPasillo = 3;
    int limiteYAleatorio =  4;
    int limiteYVentana = 5;


    //AQUI SE CONSTRUYE LA PRIMERA CLASE

    while(primeraClase[5][4] == null)

    {
      primeraClase[xp][yp] = "Asiento " + filasPrimeraClase + letraColumna;
        asiento = primeraClase[xp][yp];
        //System.out.println(asiento + "\n");
        yp++;
        //estas lineas nos permiten modificar el valor del contador a la vez que añadirlo a array en forma de String
        contadorPrimeraClase = Integer.parseInt(filasPrimeraClase);
        contadorPrimeraClase++;
        filasPrimeraClase = String.valueOf(contadorPrimeraClase);
        if(yp == limiteYprimeraClase)
            {
                yp = yp - limiteYprimeraClase;
                xp++;
                //ESTA SECCION PERMITE INTERCAMBIAR ENTRE STRING Y CHAR PARA PODER GUARDARLO COMO STRING EN EL ARREGLO
                columnaPrimeraClase = letraColumna.charAt(0);
                columnaPrimeraClase++;
                letraColumna = Character.toString(columnaPrimeraClase);
        }  

    }

    //AQUI ESTA CONSTRUIDA LA CLASE TURISTA Y DESIGNADOS SUS ASIENTOS.

    while(claseTurista[5][9] == null)

    {
        claseTurista[xpT][ypT] = "Asiento " + filasClaseTurista + letraColumnaT;
        asientoC = claseTurista[xpT][ypT];
        System.out.println(asientoC + "\n");
        ypT++;
        //estas lineas nos permiten modificar el valor del contador a la vez que añadirlo a array en forma de String
        contadorClaseT = Integer.parseInt(filasClaseTurista);
        contadorClaseT++;
        filasClaseTurista = String.valueOf(contadorClaseT);
        if(ypT == limiteYClaseT)
            {
                ypT = ypT - limiteYClaseT;
                xpT++;
                //ESTA SECCION PERMITE INTERCAMBIAR ENTRE STRING Y CHAR PARA PODER GUARDARLO COMO STRING EN EL ARREGLO
                columnaClaseT = letraColumnaT.charAt(0);
                columnaClaseT++;
                letraColumnaT = Character.toString(columnaClaseT);
        }  

    }

}

}
    
asked by Cromewar 17.11.2017 в 01:42
source

1 answer

2

You're filling your array wrong, and that's why the error ..

There are several things that seem to be wrong .. doing debug, and with a follow up, you could have discovered them alone. however, look at this:

String[][] primeraClase = new String [6][5];       
int limiteYprimeraClase = 5;
int limiteXprimeraClase = 4;
....
while(primeraClase[5][4] == null)

{
  primeraClase[xp][yp] = "Asiento " + filasPrimeraClase + letraColumna;
    asiento = primeraClase[xp][yp];
    yp++;
    ...
    if(yp == limiteYprimeraClase)
        {
            yp = yp - limiteYprimeraClase;
            xp++;

I purposely copy some lines as an example ...

note that and it is your second index of the matrix .. in this case it goes up to 5 .. what is perfect .. but x goes up to four ... this happens by chance because you never noticed if the index x passed the limit ...

But in tourist ..

String[][] claseTurista = new String [6][10];       
int limiteYClaseT = 5;
int limiteXClaseT = 9;

your index And, it goes up to 5 (when it should go up to 9) and your index x, it goes up to 9 (but you never use it) ... that is ... you wrote it backwards ... or you thought it backwards .. or whatever, but it's the other way round ...

Check that ... with a debug, following step by step what happened ... you would have noticed. I keep stressing it, because it is one of the most powerful tools that you will have when it comes to understanding what happens in these cases.

    
answered by 17.11.2017 в 05:35