I do not work the stack swing

0

Esoy doing a stack balancing program, I run the program but nothing happens, the execution time is 0 seconds, could you tell me what I'm wrong? This is the code:

package bracket.checker;

import java.util.Stack;

/**
 *
 * @author Usuario
 */
public class BracketChecker {

    private String expresion;

    public  BracketChecker(String e){
        this.expresion=e;
    }

    public boolean esValido(){
    Stack pila =new Stack();    

    for(int i = 0; i<expresion.length();i++){
        if(expresion.charAt(i)=='('){
            pila.push('(');
        }
        if(expresion.charAt(i)== ')'){

            if(pila.empty() ){
                pila.push(')');
            }else{
                pila.pop();
            }


        }
        if(expresion.charAt(i)=='{'){
            pila.push('{');
        }
        if(expresion.charAt(i)== '}'){

            if(pila.empty() ){
                pila.push('}');
            }else{
                pila.pop();
            }
        }

        if(expresion.charAt(i)=='['){
            pila.push('[');
        }
          if(expresion.charAt(i)== '['){

            if(pila.empty() ){
                pila.push('[');
            }else{
                pila.pop();
            }
        }

    }
        if(pila.empty()){
            return true;
        }
        return false;
    }
    public static void main(String[] args) {
        // TODO code application logic here
       String e1= "{([])}";
       String e2= "(][])";
       String e3= "[]{()}";

        BracketChecker parentesis = new BracketChecker(e1);
        parentesis.esValido();
    }

}
    
asked by Bruno Henríquez 25.04.2017 в 08:13
source

1 answer

0

If I have not misunderstood the code, a possible error could be in this part:

if(expresion.charAt(i)=='['){
    pila.push('[');
}
if(expresion.charAt(i)== '['){
    if(pila.empty() ){
        pila.push('[');
    }else{
        pila.pop();
    }
}

Since you always do the if comparing with '[' , I suppose that of the copy / paste you forgot to change it by ']' .

I hope I have helped you, greetings!

    
answered by 25.04.2017 в 08:20