Why does it give this error when executing the code? java.lang.Arrayindexoutofboundsexception: 0

Asked

Viewed 54 times

0

package PilhaEstatica;

public class App {

    public static void main(String[] args) {

        int numero = Integer.parseInt(args[0]);
        Pilha p = new Pilha();
        int resto;
        while (numero != 0) {
            resto = numero % 2 ; // pego o resto da divisão por 2 (é 0 ou 1)
            p.push(resto);       // armazeno na pilha
            numero = numero / 2 ;// gero um novo número, resultado da divisão dele por 2
        }

        // fase 2 = exibição de restos
        while (!p.isEmpty()) {
            resto = p.pop();    // remove da pilha o que está no topo
            System.out.println(resto);

        }
        System.out.println("Fim do Programa de Pilha!");

    }

}
package PilhaEstatica;

public class Pilha {
    private int valores[];
    private int topo;


    public Pilha() {
        this.valores = new int[10];
        topo = -1;
    }
    public void push(int elemento) {
        topo = topo + 1;
        valores[topo] = elemento;
    }
    public boolean isEmpty() {
        return( topo == -1);
    }
    public boolean isFull() {
        return (topo == 9);
    }

    public int pop() {
        int elem = valores[topo];
        topo = topo - 1;
        return elem;
    }
}
  • line 7 gives this error: int numero = Integer.parseint(args[0]);

  • java.lang.Arrayindexoutofboundsexception because of Parseint?

  • Is because of the args[0]. If you run the program without passing any parameter, args is empty then args[0] does not exist and therefore gives the index error out of Bounds

  • Um, only for example I passed '10' as parameter, and gave error the same way. Exception in thread "main" java.lang.Arrayindexoutofboundsexception: 10 at Pilhaestatica.App.main(App.java:7)

  • It’s not that. You have to pass parameters to your program: https://stackoverflow.com/q/890966

  • Um understood more or less, sorry I don’t know much of English! It would have to give me an example of passing parameter to the program?

  • I would like to pass the number 172 as parameter, which would be the form you recommend?

  • Here is an example: https://answall.com/a/88350/112052

  • Could you send an example directed to the stack of my code? in the example it says: "For every space he added an item in the "array", i.e.: String a = "hello world"; is a String. String a[] = {"ola", "world"}; is a string vector, i.e., in this example are two arrays in a vector. Note that there are also arrays with other types, such as int[] which is an array (array) of integer numbers." in my case would look like?

  • This code I am having this problem was done through a text editor, and compiled directly into the Java virtual machine by a cmd command window. So the doubt, because I’m using Spring.

  • When you run on cmd, you probably type something like java App. If you pass parameters, for example: java App 172, then "172" will be in args[0]. If you just turn java App, the array args is empty and gives the error of out of Bounds. See another example: https://www.guj.com.br/t/como-executar-aplicacao-passando-parametros/43269/2

  • So in the example it was passed like this : int code = Integer.parseint(args[1]); in my code I did this: int number = Integer.parseint(args[172]); and even so I made the exception in my spring. Exception in thread "main" java.lang.Arrayindexoutofboundsexception: 172 at Pilhaestatica.App.main(App.java:8)

  • That’s not it. Read it again that link. When you run on the command line, for example, java App algumacoisa the text "algumacoisa" is a parameter, and it is placed in the array args. Since there is only one parameter, then args only one element, which is at zero position, so you access as args[0] - see that the number that is between spoons is the position array, not value. So if you want to pass 172, run java App 172, and within the programme, in args[0] will be the 172

  • In this case, you would have to export the project and run it by cmd. As in the example of the link: java -jar oJar.jar Marcos 001

Show 9 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.