0

I've been stuck on this program for about a week and I can't see what I'm doing wrong. I can't get the operands and operators to print in the proper spot and the '(' ')' won't delete. Here's what I have so far. HELP!!!

public static int precedence(char token) {
            if (token == '^') {
                return 1; // Lowest precedence
                } else if (token == '+' || token == '-') {
                    return 2;
                    } else if (token == '*' || token == '/') {
                        return 3;
                        } else
                            return -1;
            }
        
        // method to convert infox => postfix
        public  static String infixToPostfix(String infix){
            MyStackTiara<Character> operator = new MyStackTiara<>(); // 1. Create an empty stack
            String postfix = ""; // Create an empty string stack
            MyStackTiara<Character> noNeed = new MyStackTiara<>();
            
            for (int i = 0; i < infix.length(); i++) {// Scan input from left to right
                char token = infix.charAt(i); // Current character
                
                if (token == '(') {
                    operator.push(token);
                } else if (token >= '0' || token <= '9') {
                    // Check for operand and append to string
                    postfix += token;
                }  else if (token == ')') {
                    while (!operator.isEmpty()) {
                        if (operator.peek() == '(') {
                            noNeed.push(operator.pop());
                            break;
                        } else
                            operator.pop();
                    }
                
                    //operator.pop();
                } else if (token == '*' || token == '/' || token == '+' || token == '-' || token == '^') {
                    if (token == '^') {
                        char op3 = operator.pop();
                        char operand1 = operator.pop();
                        char operand2 = operator.pop();
                        
                        String newPostfix = postfix + operand2 + operand1 + op3;
                        
                        postfix += newPostfix;
                        } else if (token == '+' || token == '-') {
                            char op2 = operator.pop();
                            char operand1 = operator.pop();
                            char operand2 = operator.pop();
                            
                            String newPostfix = postfix + operand2 + operand1 + op2;
                            
                            postfix += newPostfix;
                            } else if (token == '*' || token == '/') {
                                char op1 = operator.pop();
                                char operand1 = operator.pop();
                                char operand2 = operator.pop();
                                
                                String newPostfix = postfix + operand2 + operand1 + op1;
                                
                                postfix += newPostfix;
                                }
                        }
                }
            return postfix;
        }

When the infix is: (4-2)*(2+3)

I want it to print: 42-23+

But it's printing: 4-2)*2+3)

5
  • 2
    There are much simpler expressions which your code doesn't handle correctly -- e.g. "4-2" prints "4-2" instead of "42-". I suggest that you step through your code with the debugger while it processes a simple expression. Commented Mar 1, 2023 at 0:24
  • In particular, think about token >= '0' || token <= '9' -- what exactly does that do? Commented Mar 1, 2023 at 0:25
  • My goal was to check if the token was a digit, but now that you ask, it may make more sense to check if the token isn't an operator or parentheses. Commented Mar 1, 2023 at 20:10
  • Does token >= '0' || token <= '9' check whether the token is a digit? Is it true or false when token is '('? What does the || operator do? Commented Mar 1, 2023 at 22:56
  • And have you stepped through your code with the debugger? That is the first thing you should do. Commented Mar 1, 2023 at 22:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.