Balancing Parenthesis for Lab Project

Viewed 12
import stack.RStack;

public class Expressions {
    private RStack data;

    public Expressions() {
    }

    public boolean checkbalance(String expression) {
        char charAt;
        int i, len;
        len = expression.length();
        for(i = 0; i < len; i++) {
            charAt = expression.charAt(i);
            if(charAt == '(')
                push(charAt);
            else if(ch == ')') {
                if(isEmpty())
                    return false;
                else if((char)peek() == '(')
                    pop();
                else
                    return false;
            }
        }
        if(isEmpty())
            return true;
        else
            return false;
    }

    public int precedence(char c) {
        if((c == '*') || (c == '/'))
            return 2;
        else if((c == '+') || (c == '-'))
            return 1;
        else
            return 0;
    }

    public boolean isOperand(char c) {
        if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
            return true;
        else
            return false;
    }

    public String infixToPostfix(String infix) {
        char c;
        int len, i;
        String postfix = "";
        len = infix.length();
        for(i = 0; i < len; i++) {
            c = infix.charAt(i);
            if(isOperand(c))
                postfix = postfix + c;
            else if(c == '(')
                push(c);
            else if(c == ')') {
                while((char)peek() != '(')
                    postfix = postfix + pop();
                pop();
            } else {
                while(!isEmpty() && (precedence(c) <= precedence((char)peek())))
                    postfix = postfix + pop();
                push(c);
            }
        }
        while(!isEmpty())
            postfix = postfix + pop();
        return postfix;
    }

    public RStack getData() {
        return data;
    }

    public void setData(RStack data) {
        this.data = data;
    }
}

Any idea on why do i always get "The method push(char) is undefined for the type Expressions", "the method peek() is undefined for the type Expressions", "The method isEmpty() is undefined for the type Expressions" and "The method pop() is undefined for the type Expressions"? when im trying to balance parenthesis for lab project and I always run into this issue

1 Answers

Your calls to push and pop do not exist. Therefore, the compiler complains.

If you meant to call those methods on your own class Expressions, you’ll need to define such methods. Do like you did with defining precedence and isOperand. See The Java Tutorials by Oracle.

If you meant to call those methods on an instance of another class, such as your mysterious RStack class, then you will need a reference to such an instance before you can call the methods. See The Java Tutorials.


By the way, the char type has been essentially broken since Java 2, and legacy since Java 5. As a 16-bit value, char is physically incapable of representing most characters.

To work with individual characters, use code point integer numbers. You’ll find various codePoint methods on classes such as String, StringBuilder, and Character.

Related