Java - Postfix Notation Expression Evaluator

  • Context: Comp Sci 
  • Thread starter Thread starter schapman22
  • Start date Start date
  • Tags Tags
    Java
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
7 replies · 3K views
schapman22
Messages
74
Reaction score
0

Homework Statement



I need to write a program that evaluates a postfix notation expression using a stack.

Homework Equations



5 3 2 * + 4 - 5 + = 12

The Attempt at a Solution



Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.colorado.collections;
import java.util.Scanner;

/**
 *
 * @author schapman2344
 */
public class PostfixEvaluation
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        ArrayStack<Double> numbers = new ArrayStack();
        String input;
        double num1, num2, num3;

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter an expression in postfix notation" +
                " then press <Enter>: ");
        input = keyboard.next();
        int i = 0;

        do
        {
            if(Character.isWhitespace(input.charAt(i)))
                i++;//go to next character
            else if(Character.isDigit(input.charAt(i)))
            {
                numbers.push((double)input.charAt(i));
                i++;
            }
            else
            {
                switch(input.charAt(i))
                {
                    case '*':
                        num2 = numbers.pop();
                        num1 = numbers.pop();
                        num3 = num1*num2;
                        numbers.push(num3);
                        break;

                    case '/':
                        num2 = numbers.pop();
                        num1 = numbers.pop();
                        num3 = num1/num2;
                        numbers.push(num3);
                        break;

                    case '+':
                        num2 = numbers.pop();
                        num1 = numbers.pop();
                        num3 = num1+num2;
                        numbers.push(num3);
                        break;

                    case '-':
                        num2 = numbers.pop();
                        num1 = numbers.pop();
                        num3 = num1-num2;
                        numbers.push(num3);
                        break;
                }
                i++;
            }
        }while(i<input.length());

        System.out.println("The result is: " + numbers.pop());
    }
}

When I input a postfix expression it is outputting an incorrect number. For example when I input "5 3 2 * + 4 - 5 +" which evaluates to 12, it outputs 53.
 
Physics news on Phys.org
Changed the input to keyboard.nextLine(). But I am still getting wrong answers.
 
I suggest you add some simple debugging output, like
Code:
        System.out.println(input.charAt(i) +  " op gave " + num3);
immediately after the operations switch statement.
 
The values I'm getting back I can't make sense of. If I only enter 0 I get 48. If I enter any other single number I get 48+n. When I use an operator the numbers get even stranger.
 
Sorry just realized that these are the ascii values of the input. But I am unsure how to get it to recognize the numerical value and not the ascii value.
 
That's your problem right there. Easy enough to subtract off the ascii value of '0' from any digit character, but there's probably a valueOf or parseInt function available too.
 
Last edited:
Cool I got it working now. Thanks for your help!
 
The remaining question is: did the problem specify single digits for numerical input? otherwise you should be considering how to handle multi-character numbers.