Welcome to Dream.In.Code
Become a Java Expert!

Join 149,522 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,393 people online right now. Registration is fast and FREE... Join Now!




Postfix----Problems

 
Reply to this topicStart new topic

Postfix----Problems

cobrec
1 Jul, 2007 - 04:02 AM
Post #1

New D.I.C Head
*

Joined: 10 Mar, 2007
Posts: 29


My Contributions
Attached File  stackhmk.zip ( 1.96k ) Number of downloads: 38
Ok,

The purpose of this assignment is to make a postfix boolean expression evaluator program using the ! && and || operators.

These are the postfixes for the assignment:


true false && <-----WORKS
postfix for: true && false = false

true false ! && <-----DOESN'T
postfix for: true && !false = true

true true ! false || ! && true false && || <-----DOESN'T
postfix for: true && !(!true || false) || (true && false) = true


the first one works! However, the following two do not, and throw the following error: exception in thread "main" EmptyStackException at line 44.

One of the other requirements for this assignment is to apply the ! operator only to the most recent pushed operand....

I do not know how to accomplish this.... and am open to suggestions.

any help you can provide will be greatly appreciated.

Cobrec

CODE


//clarified version of PostFixEvaluator of section 3.8
//input a string of a valid PeckPostfix expression of integers, evaluate it.

import java.util.*;    
import javax.swing.*;

public class PeckPostfix {
    public static void main(String[] args) {
    String expression;
    String token;
    String operator;
    StringTokenizer tokens;
    Boolean result=true;
    Boolean operand1=true;
    Boolean operand2=false;
    //create empty stack of pending Boolean operands
    Stack operandStack = new Stack();  
    
    expression = JOptionPane.showInputDialog( "Enter Postfix expression" );
    tokens = new StringTokenizer(expression);
    
    while (tokens.hasMoreTokens()) {
        token = tokens.nextToken();
        
        if (!isOperator(token))   //operand
            //extract boolean from String token, create Boolean object, push onto stack
            operandStack.push(new Boolean(token));  
            
        //  printStack(operandStack);   //display stack at each step. Debugging

        else {                    //token is an operator
            //pop two ints off stack.
            //convert popped Object to Boolean, extract boolean
            operand2 = ((Boolean)operandStack.pop());
            operand1 = ((Boolean)operandStack.pop());  //"first" one
            
        if (token.equals("!"))
            result = operand1 != operand2;
        else if (token.equals("&&"))
            result = operand1 && operand2;
        else if (token.equals("||"))
            result = operand1 || operand2;
            
            operandStack.push(new Boolean(result));  
                       
        }
    }
    
    //only object in stack is the answer
    result = ((Boolean)operandStack.pop());
    JOptionPane.showMessageDialog(null,expression+" = "+result);

    System.exit(0);
    }
    
    
    static boolean isOperator(String t) {
        if (t.equals("!") || t.equals("&&") || t.equals("||"))
            return true;
        else
            return false;
    }
    
    
    static void printStack( Stack s) {
    System.out.print("Stack is: ");
        //don't normally iterate over a stack.  here for illustration purposes.
        Iterator items = s.iterator();
        while (items.hasNext())
            System.out.print(items.next()+"  ");
        System.out.println();
    }
}
/*

true false &&
    postfix for: true && false = false

true false ! &&
    postfix for:  true && !false = true
    
true true ! false || ! && true false && ||
    postfix for: true && !(!true || false) || (true && false) = true
*/




This post has been edited by cobrec: 1 Jul, 2007 - 07:11 PM
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Postfix----Problems
2 Jul, 2007 - 01:40 PM
Post #2

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
EmptyStackException at line 44. There's your clue. You're trying to pop a value from a stack that's empty. That's a runtime error, so I highly suggest you put a breakpoint before line 44 and start in debug mode to see what's going on.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:31PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month