
public class PostfixEval {

	private static Stack<String> stack;

	public static String evaluate(String postfix){


		String []array = postfix.split(" ");
		stack = new Stack<>(array.length);

		String operand1;
		String operand2;
		String res;
		for(int i=0;i<array.length;++i){
			try{
				Integer.parseInt(array[i]);
				stack.push(array[i]);

			}
			catch (Exception e) {
				if(!(array[i].equals(""))){

					operand1 =(String)stack.top();
					if(operand1!=null)
						stack.pop();
					else
						return null;

					operand2 =(String)stack.top();
					
					if(operand2!=null)
						stack.pop();
					else
						return null;
					
					res=evaluate(operand1, operand2, array[i]);
					stack.push(res);
				}
			}

		}
		if(!stack.isEmpty())
			return null;
					
		return ((String)stack.top());
	}




	public static String evaluate(String operand1,String operand2,String operation){
		String res="";
		int result;
		if(operation.equals("*")){
			result = Integer.parseInt(operand2) * Integer.parseInt(operand1);

			res=String.valueOf(result);
			return res;
		}
		if(operation.equals("/")){
			if(Integer.parseInt(operand1)!=0){
				result = Integer.parseInt(operand2) / Integer.parseInt(operand1);

				res=String.valueOf(result);
			}
			return res;
		}
		if(operation.equals("+")){
			if(Integer.parseInt(operand1)!=0){
				result = Integer.parseInt(operand2) +Integer.parseInt(operand1);

				res=String.valueOf(result);
			}
			return res;
		}
		if(operation.equals("-")){
			if(Integer.parseInt(operand1)!=0){
				result = Integer.parseInt(operand2) - Integer.parseInt(operand1);

				res=String.valueOf(result);
			}
			return res;
		}
		return res;
	}

}
