Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-18-2007, 03:08 AM   PM User | #1
johnaw24
New to the CF scene

 
Join Date: Apr 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
johnaw24 is an unknown quantity at this point
Infix to postfix converter

I have written a program to perform an infix to postfix conversion. I'm getting an empty stack exception, I can't seem to figure out where it goes empty.
Thank you in advabce for your time.

Code:
package prog17_12;

import java.util.Scanner;
import java.util.Stack;

public class InfixToPostfixConverter 
{

    //take out the infix and change it into postfix
	public static StringBuffer convertToPostfix(StringBuffer infix)
	{
		Stack<Character> charStack = new Stack<Character>();
		StringBuffer temporary = new StringBuffer("");//to store the postfix expression

		//push a left paren onto the stack and add a right paren to infix
		charStack.push('(');
		infix.append(')');

		//convert the infix expression to postfix
		for (int infixCount = 0; !charStack.isEmpty(); ++infixCount) 
		{
			if (Character.isDigit(infix.charAt(infixCount)))
				temporary.append(infix.charAt(infixCount) + " ");
			else if (infix.charAt(infixCount) == '(')
				charStack.push('(');
			else if (isOperator(infix.charAt(infixCount))) 
			{
				while ( isOperator(stackTop(charStack)) && 
						precedence(stackTop(charStack), infix.charAt(infixCount)))
					temporary.append(charStack.pop() + " ");
		
				charStack.push(infix.charAt(infixCount));
			}//end else if
			
			else if (infix.charAt(infixCount) == ')')
			{
				while (isOperator(stackTop(charStack)) || stackTop(charStack) == '(')
				{
					if (isOperator(stackTop(charStack)))
						temporary.append(charStack.pop() + " ");
					else
						charStack.pop();
				}//end while
				
			} // end else if
			
		}//end for
		return temporary;
	}//end method convertToPostfix
	
	//checks to see character is an operator
	public static boolean isOperator(char c)
	{
		if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^' || c == '%')
			return true;
		else
			return false;
	}//end method isOperator
	
	//determines if first operator(a) has equal or greater precedence over operator(b)
	public static boolean precedence(char a, char b)
	{
		String operatorString = "^*/%+-";
		if (operatorString.indexOf(a) >= operatorString.indexOf(a))
			return true;
		else
			return false;
	}//end method precedence
	
	//returns the top value of the stack without popping the stack
	public static Character stackTop(Stack<Character> stack)
	{
		return (stack.peek());
	}//end stackTop
	
	public static void main(String args[])
	{
		StringBuffer infix = new StringBuffer();
		Scanner input = new Scanner(System.in);

		//get mathematical expression from user
		System.out.println("Please input a mathematical expression");
		infix.append(input.next());
		
		System.out.println(convertToPostfix(infix).toString());
	}
	
}//InfixToPostfixConverter
here is the exception

Code:
Please input a mathematical expression
8+3-5
Exception in thread "main" java.util.EmptyStackException
	at java.util.Stack.peek(Unknown Source)
	at prog17_12.InfixToPostfixConverter.stackTop(InfixToPostfixConverter.java:73)
	at prog17_12.InfixToPostfixConverter.convertToPostfix(InfixToPostfixConverter.java:37)
	at prog17_12.InfixToPostfixConverter.main(InfixToPostfixConverter.java:85)
johnaw24 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:58 PM.


Advertisement
Log in to turn off these ads.