0
I have a program that works with the inverse Polish notation, which is, when we have an operation ((2+3)8), in Polish notation stands "23+8". I already managed to pass the operation to the Polish notation, however, I am not able to perform the operation because of a casting error, due to the function that makes the notation return a String. Follows the code:
Note: the function "fits" checks only if all open parentheses are closed.
Stack:
public class Pilha {
public Object[] pilha;
public int posicaoPilha;
public Pilha() {
this.posicaoPilha = -1;
this.pilha = new Object[1000];
}
public boolean isEmpty() {
if (this.posicaoPilha == -1) {
return true;
}
return false;
}
public int size() {
if (this.isEmpty()) {
return 0;
}
return this.posicaoPilha + 1;
}
public Object peek() {
if (this.isEmpty()) {
return null;
}
return this.pilha[this.posicaoPilha];
}
public Object pop() {
if (isEmpty()) {
return null;
}
return this.pilha[this.posicaoPilha--];
}
public void push(Object valor) {
if (this.posicaoPilha < this.pilha.length - 1) {
this.pilha[++posicaoPilha] = valor;
}
}
public void imprime() {
if (isEmpty()) {
System.out.println("Pilha vazia");
}
while (isEmpty() == false) {
System.out.print(pop() + " ");
}
}
}
Class containing the Functions:
public class Exercicio4 {
boolean encaixa(String palavra) {
int cont1 = 0, cont2 = 0;
Pilha pilha = new Pilha();
for (int i = 0; i < palavra.length(); i++) {
if(palavra.charAt(i) == '('){
char c = palavra.charAt(i);
pilha.push(c);
cont1++;
} else if(palavra.charAt(i) == ')' ) {
char c = palavra.charAt(i);
pilha.push(c);
cont2++;
}
}
return cont1 == cont2;
}
String notacao(String palavra) {
Pilha pilha = new Pilha();
String palavra2 = "";
for(int i = 0; i < palavra.length(); i++) {
char c = palavra.charAt(i);
if(Character.isDigit(c)) {
palavra2 = palavra2 + c;
} else if(c == '+' || c == '-' || c == '*' || c == '/'){
pilha.push(c);
} else if(c == ')') {
palavra2 = palavra2 + pilha.pop();
}
}
return palavra2;
}
int solucao(String palavra2) {
Pilha pilha = new Pilha();
int resultado = 0;
for(int i = 0; i < palavra2.length(); i++) {
char c = palavra2.charAt(i);
if(Character.isDigit(c)) {
pilha.push(c);
} else if(c == '+') {
/*ERRO*/
int x = (int)pilha.pop();
int y = (int)pilha.pop();
resultado = y + x;
pilha.push(resultado);
} else if(c == '-') {
int x = (int)pilha.pop();
int y = (int)pilha.pop();
resultado = y - x;
pilha.push(resultado);
} else if(c == '*') {
int x = (int)pilha.pop();
int y = (int)pilha.pop();
resultado = y * x;
pilha.push(resultado);
} else if(c == '/') {
int x = (int)pilha.pop();
int y = (int)pilha.pop();
resultado = y / x;
pilha.push(resultado);
}
}
return (int)pilha.pop();
}
}
Testapilha:
public class TestaPilha {
public static void main(String args[]) {
Exercicio4 ex4 = new Exercicio4();
String palavra = "((2+3)*8)";
if(ex4.encaixa(palavra)) {
System.out.println(ex4.notacao(palavra));
ex4.solucao(ex4.notacao(palavra));
}
}
}
Error:
Exception in thread "main" java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.Integer
at Exercicio4.Exercicio4.solucao(Exercicio4.java:53)
at Exercicio4.TestaPilha.main(TestaPilha.java:16)
You are to convert one
Object
forint
, and naturally will fail when you returnnull
which is not convertible intoint
.– Isac
@Isac and there’s a way to fix it ? Use a different stack ?
– Balenciaga
There’s always a way to fix it. Now you have to start by figuring out where you’re getting it
null
and in that place instead of directly converting first test if it is null and only converts when it is not.– Isac