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]);
– Jonatas Ferreira
java.lang.Arrayindexoutofboundsexception because of Parseint?
– Jonatas Ferreira
Is because of the
args[0]
. If you run the program without passing any parameter,args
is empty thenargs[0]
does not exist and therefore gives the index error out of Bounds– hkotsubo
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)
– Jonatas Ferreira
It’s not that. You have to pass parameters to your program: https://stackoverflow.com/q/890966
– hkotsubo
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?
– Jonatas Ferreira
I would like to pass the number 172 as parameter, which would be the form you recommend?
– Jonatas Ferreira
Here is an example: https://answall.com/a/88350/112052
– hkotsubo
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?
– Jonatas Ferreira
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.
– Jonatas Ferreira
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 inargs[0]
. If you just turnjava App
, the arrayargs
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– hkotsubo
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)
– Jonatas Ferreira
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 arrayargs
. Since there is only one parameter, thenargs
only one element, which is at zero position, so you access asargs[0]
- see that the number that is between spoons is the position array, not value. So if you want to pass 172, runjava App 172
, and within the programme, inargs[0]
will be the172
– hkotsubo
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
– Jonatas Ferreira