2

I am trying to create a program which evaluates postfix expression.For example "3500 43 12 * 47 2 / + -" .Here is my code

 public static int EvaluatePostfixExpression(String postfixExpr){
     Stack s = new Stack();
     int result = 0;
     String operand = null;


     for(int i = 0; i<postfixExpr.length();i++){
         if(Character.isDigit(postfixExpr.charAt(i)) == true){

             operand = operand + postfixExpr.charAt(i);
             if(Character.isDigit(postfixExpr.charAt(i+1)) == false){
                 s.push(operand);
                 operand = null;

                 }

         }
         if(postfixExpr.charAt(i) == '+'){
            result = result + Integer.parseInt((String) s.pop()) + Integer.parseInt((String) s.pop()) ;
         }
         if(postfixExpr.charAt(i) == '-'){
                result = result + Integer.parseInt((String) s.pop()) - Integer.parseInt((String) s.pop()) ;
             }
         if(postfixExpr.charAt(i) == '*'){
                result = result + Integer.parseInt((String) s.pop()) * Integer.parseInt((String) s.pop()) ;
             }
         if(postfixExpr.charAt(i) == '/'){
                result = result + Integer.parseInt((String) s.pop()) / Integer.parseInt((String) s.pop()) ;
             }

     }

    return result;
  } //end-EvaluatePostfixExpression

When I try to run it, there occurs an error.

Exception in thread "main" java.lang.NumberFormatException: For input string: "null12"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)

I can't find a solution.It would be great if anyone could help.

Edit: I handled the errors and my code now works;

 public static int EvaluatePostfixExpression(String postfixExpr){
     Stack s = new Stack();
     int result = 0;
     String operand = "";


     for(int i = 0; i<postfixExpr.length();i++){
         if(Character.isDigit(postfixExpr.charAt(i)) == true){

             operand = operand + postfixExpr.charAt(i);
             if(Character.isDigit(postfixExpr.charAt(i+1)) == false){
                 s.push(operand);
                 operand = "";

                 }

         }
         if(postfixExpr.charAt(i) == '+'){
                int x = Integer.parseInt((String) s.pop()) + Integer.parseInt((String) s.pop());
                result = result + x ;
                s.push(String.valueOf(x));
         }
         if(postfixExpr.charAt(i) == '-'){
                int x = Integer.parseInt((String) s.pop()) - Integer.parseInt((String) s.pop());    
                result = result + x ;
                s.push(String.valueOf(x));
             }
         if(postfixExpr.charAt(i) == '*'){
                int x = Integer.parseInt("" + s.pop()) * Integer.parseInt("" + s.pop());    
                result = result + x ;
                s.push(String.valueOf(x));
             }
         if(postfixExpr.charAt(i) == '/'){
                int x = Integer.parseInt((String) s.pop()) / Integer.parseInt((String) s.pop());    
                result = result + x ;
                s.push(String.valueOf(x));
             }

     }

    return result;
  } 

but now the result is wrong.It should be 2961 but I am getting -1952.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Yunus Emre Güler
  • 387
  • 1
  • 6
  • 16

2 Answers2

1

A slight rewrite:

public static int evaluatePostfixExpression(String postfixExpr) {
    Stack<Integer> s = new Stack<Integer>();
    String[] items   = postfixExpr.split(" ");

    for (String item : items) {
        try {
            s.push(Integer.valueOf(item));
        } catch (NumberFormatException e) {
            Integer value1 = s.pop();
            Integer value2 = s.pop();

            switch (item) {
                case "+":
                    s.push(value2 + value1);
                    break;
                case "-":
                    s.push(value2 - value1);
                    break;
                case "*":
                    s.push(value2 * value1);
                    break;
                case "/":
                    s.push(value2 / value1);
                    break;
            }
        }
    }

    return s.pop();
}

Improvements to make to this would be:

  • Assumes that each item is either a valid Integer or a valid operand
  • Assumes that the Stack contains 2 items when receiving an operand
  • Should check that the stack only has 1 item before returning
Phylogenesis
  • 7,775
  • 19
  • 27
0

Try this one:

 String operand = "";

Change it also when

if(Character.isDigit(postfixExpr.charAt(i+1)) == false){
    s.push(operand);
    operand = "";
}
Paco Abato
  • 3,920
  • 4
  • 31
  • 54
  • Check the number of elements that you insert into the stack. It seems that you are doing more `s.pop()` than `s.push()`. – Paco Abato Dec 16 '14 at 09:44
  • Take a loot at http://stackoverflow.com/questions/13063282/emptystackexception maybe it contains something useful for you. – Paco Abato Dec 16 '14 at 09:47
  • I think that you should accept the answer and make a new question as it is a totally diferent issue now. – Paco Abato Dec 16 '14 at 10:01