Expression Calculator - Expression breaking problem

5

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 + 5

     

java.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)

    
asked by anonymous 18.05.2016 / 00:29

1 answer

0

A colleague of mine made a calculator in Java and used a different way than yours. He used a method called "eval" that turns a string into executable code for Javascript. I'll show you an example:

public static void main(String[] args) 
{
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("nashorn");

    String teste = "print((5-2) * 3);";
    try 
    {
     engine.eval(teste);
    } 
    catch (ScriptException e)
    {
     e.printStackTrace();
 }
  

On the console will appear the 9 result.

I do not know if this answers your question, but I hope I have helped.

    
18.05.2016 / 01:50