I'm doing college and I have to do an Expression Calculator in java , ie basically if you write (5 + 5) / 2 or anything like it, it has to give you the result right.
I did not have problems with the use of stacks and queues, but I came across a problem when breaking my expression. Here is the method and exception that occurred to me:
private static String[] quebraExressao(String expressao)
{
expressao = expressao.replaceAll(" ", "").replaceAll("", " ");
String regex = "[0-9]";
String[] elementos = expressao.split(" ");
String[] elementos_retorno = new String[elementos.length];
int j = 0;
for (int i =0; i < elementos.length; i++)
{
if (elementos[i].matches(regex))
{
if (elementos[i+1].matches(regex))
{
elementos_retorno[j] = elementos[i] + elementos[i+1];
i++;
}
else
{
elementos_retorno[j] = elementos[i];
}
}
else
{
elementos_retorno[j] = elementos[i];
}
j++;
}
return elementos_retorno;
}
And finally I received this exception
Enter the expression: 5 + 5
Expression: 5 + 5java.lang.ArrayIndexOutOfBoundsException: 4 0.0 at. calculadorapoo.CalculoPolones.calcula (CalculationPolones.java:14) at calculadorapoo.CalculadoraPOO.main (CalculadorPOO.java:18) BUILT WITH SUCCESS (total time: 5 seconds)
I'm using netbeans to do my program and I do not quite understand where my array popped up.
I'd like to know what the best possible solution is, I've been learning Java a short time ago. If you need the rest of the program, I can post here. Thank you.
EDITED: I was able to solve the problem, but now I have NullPointer problem. Here is the code and error:
public Double calcular() throws Exception
{
int tamanhoFila = this.expressao.trim().length();
Fila fila = new Fila<>(tamanhoFila);
Pilha pilha = new Pilha<>(tamanhoFila);
String regex = "[0-9]";
String[] elementos = quebraExressao(this.expressao.trim());
for (String token : elementos)
{
if (!token.equals(""))
{
if (token.matches(regex)) // se for numero
{
fila.emfila(token);
}
else // se for operador
{
if (pilha.isVazia())
{
pilha.insere(token);
}
else
{
String ultimoOperador = pilha.getUltimoElemento().toString();
if (Tabela.devoDesempilhar(ultimoOperador.charAt(0), token.charAt(0)))
{
if (token.equals(")"))
{
while(!pilha.getUltimoElemento().equals("("))
{
fila.emfila(pilha.retira());
}
pilha.retira(); // para retirar o (
}
else
{
while(Tabela.devoDesempilhar(ultimoOperador.charAt(0), token.charAt(0)))
{
fila.emfila(pilha.retira());
if (pilha.isVazia())
{
pilha.insere(token);
break;
}
else
ultimoOperador = pilha.getUltimoElemento().toString();
}
}
}
else
{
pilha.insere(token);
}
}
}
}
}
Apparently it occurred in two places; I still do not know if it's because of the very first one, but I'll put the other method.
public double calcula(String expressao)
{
double resultado = 0.0;
Expressao exp = new Expressao(expressao);
try
{
System.out.println("Expressao: " + expressao);
resultado = exp.calcular();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultado;
}
Here's NULLPOINTER and its location
Enter the expression: 5 + 5
Expression: 5 + 5 java.lang.NullPointerException 0.0 at calculadorapoo.Expresión.calcular (Expressao.java:66) at calculadorapoo.CalculoPolones.calcula (CalculationPolones.java:14) at calculadorapoo.CalculadoraPOO.main (CalculadorPOO.java:18) CONSTRUCTED WITH SUCCESS (total time: 2 seconds)