Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Error:Attemp to pop an empty stack

807588Mar 23 2009 — edited Mar 23 2009
Error:Attempt to pop an empty stack: I am working on this program for most of yesterday up this present moment and I can't figure out why I am getting this error. I canot understand why "top" is null after I have pushed the values on the stack which I was able to print and varify.

1. The program compiles ok.
2. I try hard not to put all my code, but to present the picture I have to.
3. What I am doing I am reading in an expression, place it in a binaryTree and visit the nodes in post order(This I have done correctly)
4. The problem is when I try to evaluate the expression (51 3 15 * - 9 65 13 / - +) which is the out put of the above.
5. The problem is when I try to pop() values of the stack, it is indicating it is empty. I can't understand this when I have already push the values on the stack. There is something I am missing and I can't seem to figure appreciate your.

code for part is correct except for the evaluation method. Your assistanc appreciated.
public class Node {
	NodeDatas data;
	Node next;
	
    public Node(NodeDatas d) {
    	data = d;
    	next = null;
    }
}// end class Node
public class NodeDatas {
	char c;
	int num;
	
    public NodeDatas(char c) {
    	this.c = c;
    }
    
    public NodeDatas (int n){
    	num = n;
    }
    
    public char getData(){
    	return c;
    }
    
    public int getIntData(){
    	return num;
    }    
}
public class Stack {
	Node top = null;
	
    public Stack() {
    }
    
    public boolean empty(){
    	return top == null;
    }
    
    public void push(NodeDatas n){
    	Node np = new Node(n);
    	np.next = top;
    	top = np;
    }// end push
    
    
    public NodeDatas pop(){
    	if(this.empty()){
    		System.out.printf("\n Attempt to pop an empty stack \n");
			System.exit(2);
		}
    	NodeDatas hold = top.data;
    	top = top.next;
    	return hold;
    }//end pop    
}
import java.io.*;
import java.util.*;

class TreeNode{
	NodeData data;
	TreeNode left, right;
	
	TreeNode(NodeData d){
		data = d;
		left = right = null;
	}
}//end TreeNodeClass

class NodeData{
	String word;
		
	public NodeData(String w){
		word = w;
	}
	
		
	public void visit(){
	System.out.printf("%s",word);
	}
	
	public void evaluate(){////////////////////////////////////////////here is where my evaluation of the expression starts.
	
		int j, a, b, c;
		Stack S = new Stack();
		
			char p = word.charAt(0);//word is a string
			
			
			if(Character.isDigit(p))//if p is a digit convert word sent to push()
	
				S.push(new NodeDatas(Integer.parseInt(word)));//push values on stack
		else{
				
				b = S.pop().getIntData();// when I try to pop, says stack is empty
				a = S.pop().getIntData();//
				
				if(p == '+')
					c = a + b;
				else
				if(p == '-')
					c = a - b;
				else
				if(p == '*')
					c = a * b;
				else
					c = a/b;
				S.push(new NodeDatas(c));		
			}// end else
		//System.out.println(S.pop().getIntData());
	}
}//end NodeData1



class BinaryTree{
	TreeNode root;
	
	BinaryTree(){
		root = null;
	}
	
	public BinaryTree(Scanner in){
		root = buildTree(in);
	}
	
	public static TreeNode buildTree(Scanner in){
	
		String str = in.next();
		if(str.equals("@"))return null;
		TreeNode p = new TreeNode(new NodeData(str));
		p.left =  buildTree(in);
		p.right = buildTree(in);
		return p;
	}//end buildTree
	
	public void postOrder(){
		postOrderTraversal(root);
	}
	
	
	public void postOrderTraversal(TreeNode node){
		int i = 0;
		if(node!=null){
			postOrderTraversal(node.left);
			postOrderTraversal(node.right);
			node.data.visit();
			node.data.evaluate();/////////////////////////////Here I am sending each string one at a time
		}
	}//end postOrder
}//end BinaryTree

class Nichole{
	public static void main(String[] args)throws IOException
	{
		Scanner in  = new Scanner(new FileReader("zeth.txt"));
		BinaryTree bit = new BinaryTree(in);
		bit.postOrder();
	}//end main	
}//end class
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 20 2009
Added on Mar 23 2009
1 comment
464 views