Java中的括号检查

Java中的括号检查,java,arrays,stack,Java,Arrays,Stack,我试着看看我的等式在括号里是否平衡。但是我在方程方面遇到了麻烦,x=(1+2))+3。它总是有效的,即使它不是。有什么帮助吗 public boolean checkEquation(String infix){ for(int j=0;j<infix.length();j++){ // loop until the end of the line for a string (expression) // Check for parentheses i

我试着看看我的等式在括号里是否平衡。但是我在方程方面遇到了麻烦,
x=(1+2))+3
。它总是有效的,即使它不是。有什么帮助吗

public boolean checkEquation(String infix){
    for(int j=0;j<infix.length();j++){    // loop until the end of the line for a string (expression)
    // Check for parentheses
        if( infix.charAt(j)=='('){
            operatorStack.push(infix.charAt(j)); // push the parentheses to stack
        } 
        // Check for braces
        else if( infix.charAt(j)=='[' ){
            operatorStack.push(infix.charAt(j)); // push the braces to stack
        }
        // Check for bracket
        else if( infix.charAt(j)=='{' ){
            operatorStack.push(infix.charAt(j)); // push the bracket to stack
        }

        /* for reverse cases */
        // Check for reverse parentheses
        else if( infix.charAt(j)==')' ){
            if(operatorStack.isStackEmpty()){
                System.out.println(operatorStack.pop());
                return false;
            }
            if(operatorStack.peek()!='('){
                System.out.println(operatorStack.pop());
                return false;
            }
        }
        // Check for reverse braces
        else if( infix.charAt(j)==']' ){
            if(operatorStack.isStackEmpty()){
                return false;
            }
            if(operatorStack.peek()!='['){
                return false;
            }
        }
        // Check for reverse bracket
        else if( infix.charAt(j)=='}' ){
            if(operatorStack.isStackEmpty()){
                return false;
            }
            if(operatorStack.peek()!='{'){
                return false;
            }
        }
            // ignore other characters for now
    } // close for loop

    return operatorStack.isStackEmpty();

} // end of checkEquation method
公共布尔校验方程(字符串中缀){

对于(int j=0;j这是适用于我的示例的已更改代码。有关更改,请参阅添加的注释

public static Scanner input;
public static Stack<Character> operatorStack;

public static void main(String[] args) {
    operatorStack = new Stack<>(); // <- I used Stack, because I don't know what kind of stack you used
    System.out.println(checkEquation("x=(1+2))+3"));        
    System.out.println(checkEquation("x=((1+2))+3"));
    System.out.println(checkEquation("x={[((1+2))]+3}"));
    System.out.println(checkEquation("x=()   ()   ()"));
    System.out.println(checkEquation("x=()   )(   ()"));
    System.out.println("-----");
    System.out.println(checkEquation("x=((1+2)*3"));
    System.out.println(checkEquation("x=(1+2))*3"));
    System.out.println(checkEquation("x=(z*j)/(b*8)^2"));
    System.out.println(checkEquation("x=((p*2)"));
}

public static boolean checkEquation(String infix) { // <- don't mind that "static". You don't need that
    operatorStack.clear(); // <- clear the stack to remove old data
    for (int j = 0; j < infix.length(); j++) {

        // check for opening parentheses omitted

        else if (infix.charAt(j) == ')') {
            if (operatorStack.isEmpty()) { // <- different check for empty stack (due to the different stack type)
                // System.out.println(operatorStack.pop()); // <- removed unwanted pop() call
                return false;
            }
            if (operatorStack.pop() != '(') { // <- changed from peek() to pop()
                // System.out.println(operatorStack.pop());
                return false;
            }
        }
        else if (infix.charAt(j) == ']') {
            if (operatorStack.isEmpty()) { // <- different check for empty stack (due to the different stack type)
                return false;
            }
            if (operatorStack.pop() != '[') { // <- changed from peek() to pop()
                return false;
            }
        }
        else if (infix.charAt(j) == '}') { 
            if (operatorStack.isEmpty()) { // <- different check for empty stack (due to the different stack type)
                return false;
            }
            if (operatorStack.pop() != '{') { // <- changed from peek() to pop()
                return false;
            }
        }
    }
    return operatorStack.isEmpty();
}

如果您发现一个表达式不起作用,请随意添加注释。

我建议您学习在IDE中使用调试器。只需稍加努力,您应该能够跟踪代码中的问题所在。如果您使用
peek()
则将字符留在堆栈中。这样,第二个
还使用第一个
来进行检查。您应该使用
pop()
来代替。code-学徒:尝试使用调试器,但无法找到解决方案。Tom-我尝试使用pop(),也不适用于上述表达式,然后发布一个它不适用于哪个表达式的示例。
false
true
true
true
false
-----
false
false
true
false