How to solve prefix and postfix expressions in Java