Java - Postfix Notation Expression Evaluator

  • Context: Comp Sci 
  • Thread starter Thread starter schapman22
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around the implementation of a program that evaluates postfix notation expressions using a stack in Java. Participants explore issues related to input handling and output correctness, focusing on debugging and potential improvements to the code.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The initial implementation outputs incorrect results, such as returning 53 instead of 12 for the input "5 3 2 * + 4 - 5 +".
  • A participant suggests changing the input method to keyboard.nextLine() to address the issue, but the problem persists.
  • Another participant recommends adding debugging output to track the operations and results during evaluation.
  • One participant notes confusion over receiving ASCII values instead of numerical values when entering single digits.
  • A later reply identifies the ASCII value issue and suggests subtracting the ASCII value of '0' to convert characters to their numerical equivalents, mentioning the existence of functions like valueOf or parseInt for this purpose.
  • One participant reports successfully resolving their issue after receiving help.
  • A final question is raised regarding whether the problem specified single digits for numerical input, highlighting the need to consider multi-character numbers in the implementation.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the handling of multi-character numbers, and the discussion remains unresolved regarding the specifications of the input format.

Contextual Notes

There are limitations regarding the handling of multi-digit numbers and the dependence on character encoding for numerical input. The discussion reflects uncertainty about the initial problem statement's requirements.

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
3K
  • · 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
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K