samedi 26 mai 2018

Else if not working on android studio

If statement is always working. So else if statement never works. operators stack is always empty. So i can not calculate result.

  public static Double evaluate(String exp) {
    char[] tokens = exp.toCharArray();
    Stack<Double> operands = new Stack<>();
    Stack<Character> operators = new Stack<>();

    for (int i = 0; i < tokens.length; i++) {
        if (isOperand(tokens[i])) {
            StringBuffer sb = new StringBuffer();
           while (i < tokens.length && isOperand(tokens[i])) {
                sb.append(tokens[i++]);
                operands.push(Double.parseDouble(sb.toString()));
            }
        } 
        else if (isOperator(tokens[i])) {
            while (!operators.empty() && hasHigherPrec(tokens[i], operators.peek())){
                operands.push(performOperation(operators.pop(), operands.pop(), operands.pop()));
            }
            operators.push(tokens[i]); 
        }

    }
    while (!operators.empty()){
        operands.push(performOperation(operators.pop(), operands.pop(), operands.pop()));
    }

    return operands.pop();
}

Aucun commentaire:

Enregistrer un commentaire