Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Java - PostFix ecaluator

  1. Mar 28, 2012 #1
    1. The problem statement, all variables and given/known data

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

    2. Relevant equations

    5 3 2 * + 4 - 5 + = 12

    3. The attempt at a solution

    Code (Text):
    /*
     * 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.
     
  2. jcsd
  3. Mar 28, 2012 #2
    Changed the input to keyboard.nextLine(). But I am still getting wrong answers.
     
  4. Mar 28, 2012 #3
    I suggest you add some simple debugging output, like
    Code (Text):
            System.out.println(input.charAt(i) +  " op gave " + num3);
    immediately after the operations switch statement.
     
  5. Mar 28, 2012 #4
    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.
     
  6. Mar 28, 2012 #5
    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.
     
  7. Mar 28, 2012 #6
    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: Mar 28, 2012
  8. Mar 28, 2012 #7
    Cool I got it working now. Thanks for your help!
     
  9. Mar 28, 2012 #8
    The remaining question is: did the problem specify single digits for numerical input? otherwise you should be considering how to handle multi-character numbers.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook