-6
\$\begingroup\$
public static StringBuffer infixToPostfix(StringBuffer infix) throws InvalidCharacterException
        {
            StringBuffer postfix = new StringBuffer("");


        Stack<String> myStack = new Stack<String>();
        myStack.push("(");
        infix.append(')');

        for(int i = 0; i < infix.length(); i++)
        {

            if(infix.charAt(i) == ' ')
            {
                //System.out.println("Space!");
            }
            else if(Character.isDigit(infix.charAt(i)) == true)
            {
                postfix.append(infix.charAt(i) + " ");
            }
            else if(infix.charAt(i) == '(')
            {
                myStack.push("(");
            }
            else if(infix.charAt(i) == ')')
            {

                while(myStack.peek()!="(")
                {
                    postfix.append(myStack.pop() + " ");

                }
                myStack.pop();
            }
            else if(isOperator(infix.charAt(i) + "") == true)
            {
                String peekedItem = myStack.peek();

                if(isOperator(peekedItem) == true)
                {

                    if(getPrecedence(infix.charAt(i) + "") <= getPrecedence(peekedItem))
                    {
                        String poppedOp = myStack.pop();
                        if(poppedOp == "+")
                        {
                            postfix.append("+ ");
                        }

                        else if(poppedOp == "-")
                        {
                            postfix.append("- ");
                        }

                        else if(poppedOp == "*")
                        {
                            postfix.append("* ");
                        }

                        else if(poppedOp == "/")
                        {
                            postfix.append("/ ");
                        }

                        else if(poppedOp == "%")
                        {
                            postfix.append("% ");
                        }


                        String op = String.valueOf(infix.charAt(i));
                        myStack.push(op);
                    }
                }
                else if(isOperator(peekedItem)==false)
                {
                    String op = String.valueOf(infix.charAt(i));
                    myStack.push(op);
                }

            }
            else
                throw new InvalidCharacterException(infix.charAt(i));
        }

        return postfix;
    }



public static double evaluatePost(StringBuffer postfix)
    {
        String str = new String(postfix);

        str = str.replaceAll(" ", "");

        postfix = new StringBuffer(str);
        postfix.append(")");

        Stack<Double> anotherStack = new Stack<Double>();

        double answer = 0;
        for(int k = 0; postfix.charAt(k) != ')'; k++)
        {

            if(Character.isDigit(postfix.charAt(k)) == true)
            {
                anotherStack.push(Double.parseDouble(postfix.charAt(k)+""));

            }
            else if(isOperator(postfix.charAt(k)+"")==true)
            {
                double x = anotherStack.pop();


                double y = anotherStack.pop();


                char op = postfix.charAt(k);


                if(op=='+')
                {
                    answer = (x+y);
                    anotherStack.push(answer);
                    answer = 0;
                }

                else if(op=='-')
                {
                    answer = (y-x);
                    anotherStack.push(answer);
                    answer = 0;
                }

                else if(op=='*')
                {
                    answer = (x*y);
                    anotherStack.push(answer);
                    answer = 0;
                }

                else if(op=='/')
                {
                    answer = (y/x);
                    anotherStack.push(answer);
                    answer = 0;
                }

                else if(op=='%')
                {
                    answer = (x%y);
                    anotherStack.push(answer);
                    answer = 0;
                }

            }

        }
        double finalAnswer = anotherStack.pop();

        return finalAnswer;


    }
\$\endgroup\$

closed as off-topic by 200_success, amon, Anirban Nag 'tintinmj', Simon Forsberg, Kinjal Nov 21 '13 at 13:41

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After getting your code to work, you may edit this question seeking a review of your working code." – 200_success, amon, Anirban Nag 'tintinmj', Simon Forsberg, Kinjal
If this question can be reworded to fit the rules in the help center, please edit the question.

1
\$\begingroup\$

When doing String comparisons, *DO NOT USE == *

Change your lines:

if(poppedOp == "+") {
     postfix.append("+ ");
}
.....

to be

if("+".equals(poppedOp)) {
     postfix.append("+ ");
}
.....
\$\endgroup\$
0
\$\begingroup\$

Instead of those cascade ifs, use switches

Replace all the if(foo == true) with if(foo)

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.