-2

I am making a post fix calculator and i can not figure out how to do functions without having an equal sign. For example " a = 3 5 + " will print 8 , but " 3 5 + " will make the program crash. Here is what i have so far

if (values.peek().matches("[a-z]") || values.search("=") > -1 )
{
    String a = values.pop();
    values.pop();
    double answer = compute(values);
    memory.put(a, answer);
    var.add(a);
    System.out.println("   " + answer);
}
else
{
    double answer = compute(values);
    System.out.println(answer);
}
  • 1
    Could you please post the exception, when the program crashes. – Ahmet Emre Kilinc Nov 14 '17 at 19:03
  • Exception in thread "main" java.util.EmptyStackException at java.util.Stack.peek(Stack.java:102) at java.util.Stack.pop(Stack.java:84) at Program6.compute(Program6.java:93) at Program6.calculate(Program6.java:71) at Program6.main(Program6.java:159) – mcgoogle63 Nov 14 '17 at 19:11
  • 1
    We can't tell which line that is. And don't add information in the comments; [edit] your question to add it in. – shmosel Nov 14 '17 at 19:18

1 Answers1

0

I guess your parsing of input has some problem. If you want to split the input by space, try this way:

String str = " 3 5 + ";
String[] splited = str.split("\\s+");

Inspired by the first answer of this question:

NoSegfault
  • 675
  • 1
  • 9
  • 14