import java.io.*;
import java.util.*;



public class InfixToPostixEvaluate {


	private static final String operators = "-+/*";
	private static final String operands = "0123456789";

	private static int getPrecedence(char operator) {
		int ret = 0;
		if (operator == '-' || operator == '+') {
			ret = 1;
		} else if (operator == '*' || operator == '/') {
			ret = 2;
		}
		return ret;
	}

	private static boolean operatorGreaterOrEqual(char op1, char op2) {
		return getPrecedence(op1) >= getPrecedence(op2);
	}

	private static boolean isOperator(char val) {
		return operators.indexOf(val) >= 0;
	}

	private static boolean isOperand(char val) {
		return operands.indexOf(val) >= 0;
	}

	public static String convert2Postfix(String infixExpr) {
		char[] chars = infixExpr.toCharArray();
		Stack<Character> stack = new Stack<Character>();
		StringBuilder out = new StringBuilder(infixExpr.length());

		for (char c : chars) {
			if (isOperator(c)) {
				while (!stack.isEmpty() && stack.peek() != '(') {
					if (operatorGreaterOrEqual(stack.peek(), c)) {
						out.append(stack.pop());
					} else {
						break;
					}
				}
				stack.push(c);
			} else if (c == '(') {
				stack.push(c);
			} else if (c == ')') {
				while (!stack.isEmpty() && stack.peek() != '(') {
					out.append(stack.pop());
				}
				if (!stack.isEmpty()) {
					stack.pop();
				}
			} else if (isOperand(c)) {
				out.append(c);
			}
		}
		while (!stack.empty()) {
			out.append(stack.pop());
		}
		return out.toString();
	}

	public static int evaluatePostfix(String postfixExpr) {
		char[] chars = postfixExpr.toCharArray();
		Stack<Integer> stack = new Stack<Integer>();
		for (char c : chars) {
			if (isOperand(c)) {
				stack.push(c - '0'); 
			} else if (isOperator(c)) {
				int op1 = stack.pop();
				int op2 = stack.pop();
				int result;
				switch (c) {
				case '*':
					result = op1 * op2;
					stack.push(result);
					break;
				case '/':
					result = op2 / op1;
					stack.push(result);
					break;
				case '+':
					result = op1 + op2;
					stack.push(result);
					break;
				case '-':
					result = op2 - op1;
					stack.push(result);
					break;
				}
			}
		}
		return stack.pop();
	}

	public int evalInfix(String infix) {
		return evaluatePostfix(convert2Postfix(infix));
	}
	
	public static void main(String[] args)throws IOException{
		
		String infix;

		
		BufferedReader input = new BufferedReader (new InputStreamReader(System.in));


		System.out.print("\nEnter the algebraic expression in infix: ");
		infix = input.readLine();


		System.out.println("The expression in postfix is:" + convert2Postfix(infix));


		System.out.println("The answer to the equation is: " + evaluatePostfix(infix)+ "\n");
		
	}
}

Ihave problem with this code. It has this errors:

Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Stack.java:79)
at java.util.Stack.pop(Stack.java:61)
at InfixToPostixEvaluate.evaluatePostfix(InfixToPostixEvaluate.java:77)
at InfixToPostixEvaluate.main(InfixToPostixEvaluate.java:121)

Recommended Answers

All 3 Replies

The problem is in the evaluatePostfix method, as the error says.
Probably because the program goes to else if { ... } and you try to do a
int op1 = stack.pop()
If the stack is empty how can you pop something from it?
First check if the stack has values before calling pop. I am not very familiar with the Stack class but here is the API:
Stack
There is an empty() method that checks if the stack is empty or not

hello,,is this a student of ma'am romz???

helo ate. classmate baya ta!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.