error in java = Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

0

After researching and not finding a solution to this problem I decided to resort to this forum the code is simple and the error maybe is silly but I am just beginning to write code, I leave the code and the error attached, and if I they can explain everything they know about it, they would greatly appreciate it since I am interested in learning well how everything works (it is written in java)

Here I leave the written code:

   Cven = JOptionPane.showInputDialog("ingrese la cantidad de vendedores ");
   int cantVende = Integer.valueOf(Cven);

   int importe;
   int cont;
   int suma = 0;
   String vendedores[] = new String[cantVende];
   int importeTotal[] = new int[cantVende];
   int i;


    for ( i = 0; i < cantVende; i++) {
        int ii = i + 1 ;

     String vendedor = JOptionPane.showInputDialog("ingrese el codigo del " + ii + "° vendedor ingrese \"x\" si desea finalizar ahora " );

        if ("x".equals(vendedor)) {
           String a = JOptionPane.showInputDialog("no a terminado de ingresar todos los vendedores, ingrese \"V\"si desea finalizar, de lo contrario ingrese cualquier otro caracter ");
            if ("v".equals(a)) {
                break;
            } 
            else{
                i -= 1 ;
                continue;
            }     
                                  }
        vendedores[i] = vendedor;

        importe = 1;
        suma = 0;
        cont = 0;
        while (importe != 0) {
          cont++;
          String importt =  JOptionPane.showInputDialog("ingrese el "+ cont +"° importe del vendedor "+ vendedores[i] + " ingrese 0 para finalizar " );
          importe = Integer.valueOf(importt);

          suma += importe; 
                            }    
      importeTotal[i] = suma;
    }

    for (int a = 0; a <= i; a++) {

        JOptionPane.showMessageDialog(null," es "+ "el importe de "+vendedores[a] );// esta es la linea que da error 

    }

The error is this:

  

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1     at   buyers.and.sales.CompradoresYVentas.main (Buyers and Sales.java:65)

    
asked by nicolas gigena 03.05.2018 в 04:46
source

2 answers

0

This response should be a comment, but by format I leave it here.

At the end of the for loop:

for ( i = 0; i < cantVende; i++) { ... }

The value of i will be equal to cantVende

So in your last for :

for (int a = 0; a <= i; a++) { ... }

In your last iteration a will also be equal to cantVende and with that vendedores[a] will give you a ArrayIndexOutOfBoundsException .

Just use a < i

    
answered by 03.05.2018 / 14:59
source
0

As the partner said, the exception corresponds to trying to access an array position outside its limits, such as array [-1].

Some recommendations that I can give you are the following:

-The break and continue are the devil, try to reorganize your algorithm so you do not have to use them.

-Use more descriptive variable names, "ii" is not a descriptive name at all, and the name "vendor" for a variable that will store a code or an "x" string is also not suitable. Do not be afraid to use long variable names if that will make your code more readable.

-It is not considered good practice to make methods or functions of more than 7-10 lines, extract blocks of code with specific functions and create new functions that have a single purpose.

-Don't use what we call "Magic Strings", for example, if in your function, a string "x" is used to finish the execution, do not pass that "x" as you have done, save that String in a variable with a descriptive name, for example "close", "finish"

    
answered by 03.05.2018 в 09:57