Comp Sci Java - Postfix Notation Expression Evaluator

  • Thread starter Thread starter schapman22
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
The discussion focuses on creating a Java program to evaluate postfix notation expressions using a stack. The initial implementation incorrectly outputs ASCII values instead of numerical results due to not converting character digits properly. Suggestions include using `Character.getNumericValue()` or subtracting the ASCII value of '0' to obtain the correct numerical values. The user successfully resolves the issue but raises a question about whether the problem specifies single-digit inputs, indicating the need for handling multi-character numbers. The conversation emphasizes debugging techniques and proper data type handling in Java.
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K