为什么我在计算postfix last运算符时出错?

时间:2016-02-25 04:47:54

标签: java stack

该项目要求使用Stack来计算后缀表达式,特别是这个:

5.0 3.5 - 1.2 /

现在我的代码适用于此后缀表达式:

2 3 +

我错过的是给我以下错误?

  

线程中的异常" main" java.lang.NumberFormatException:为空   字符串
      at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)     在sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)at at   java.lang.Double.parseDouble(Double.java:538)at   java.lang.Double。(Double.java:608)at   project.Calculator.processIn(Calculator.java:55)at   project.Project_main.main(Project_main.java:25)Java结果:1

package project;

import java.util.ArrayList;
import java.util.Stack;



public class Calculator {
    Stack<Double> calcStack = new Stack<>();
    ArrayList<String> operators = new ArrayList<>();

    public Calculator() {
        operators.add("+");
        operators.add("-");
        operators.add("/");
        operators.add("*");
    }

    public double processIn(String expression){
        String[] express = expression.split("");

        String temp = "";
        for(String j : express){
            if(operators.contains(j)){
                System.out.println("Operator Reached: " + j);
                double operand2 = calcStack.pop();
                double operand1 = calcStack.pop();

                double tmp = 0.0;
                if(j.compareTo("*") == 0){
                    tmp = operand1 * operand2;
                    System.out.println("Performing multiplication on " + operand1 + " and " + operand2);
                    calcStack.push(tmp);
                }
                else if(j.compareTo("/") == 0){
                    tmp = operand1 / operand2;
                    System.out.println("Performing division on " + operand1 + " and " + operand2);
                    calcStack.push(tmp);
                }
                else if(j.compareTo("-") == 0){
                    tmp = operand1 - operand2;
                    System.out.println("Performing subtraction on " + operand1 + " and " + operand2);
                    calcStack.push(tmp);
                }
                else if(j.compareTo("+") == 0){
                    tmp = operand1 + operand2;
                    System.out.println("Performing addition on " + operand1 + " and " + operand2);
                    calcStack.push(tmp);
                }
                System.out.println("Pushing result to stack: " + tmp);

            }
            else if(j.compareTo(" ") == 0){
                double newOperand = new Double(temp);
                temp = "";
                System.out.println("Pushing to stack: " + newOperand);
                calcStack.push(newOperand);
            }
            else{
                temp = temp + j;
                System.out.println("Current temp value: " + temp);
            }
        }
        return calcStack.pop();
    }
}

2 个答案:

答案 0 :(得分:0)

我认为这可以解决您的问题。 与&#34;分开&#34;(空格)而不是&#34;&#34;

String[] express = expression.split(" ");

并将您的其他内容更改为

else {
    double newOperand = new Double(j);
    System.out.println("Pushing to stack: " + newOperand);
    calcStack.push(newOperand);
}

并删除else if if bellow

else if(j.compareTo(" ") == 0){
    double newOperand = new Double(temp);
    temp = "";
    System.out.println("Pushing to stack: " + newOperand);
    calcStack.push(newOperand);
}

这样你可以直接在堆栈中添加数字。 你的错误发生了,因为你在操作后如何处理空间。当你有数字后跟数字时,先调用else,然后调用space。所以你的临时值中有一个数字。但是在完成一个操作后你会立刻达到一个空间增益,但是你的temp没有值,所以新的Double(temp)抛出一个异常,因为temp不是一个数字。 所以如果你想保持它的原样,修复代码的另一种方法是使用try和catch如下。

else if(j.compareTo(" ") == 0){
    try{
    double newOperand = new Double(temp);
    temp = "";
    System.out.println("Pushing to stack: " + newOperand);
    calcStack.push(newOperand);
    }catch(NumberFormatException ex){
        //skip
    }
}
通过这种方式,您可以在不破坏代码的情况下捕获错误。但我建议第一个解决方案。

答案 1 :(得分:0)

请将字符串拆分为空格“”,如Shawn建议的那样expression.split(" ") (但这假定您的表达式始终包含运算符和操作数之间的空格)。这将为您提供两种类型的标记/字符串操作数和运算符。这是更简化的代码实现。请注意expression.trim()操作以避免任何尾随空格。

[顺便说一句,我已经尝试过eclipse并且有效:)]

public double processIn(String expression)
{
    expression = expression.trim();
    System.out.println("Trimmed expressoin : " + expression);
    String[] express = expression.split(" ");

    for(String j : express){

        if(operators.contains(j)){
            System.out.println("Operator Reached: " + j);
            double operand2 = calcStack.pop();
            double operand1 = calcStack.pop();

            double tmp = 0.0;
            if(j.compareTo("*") == 0){
                tmp = operand1 * operand2;
                System.out.println("Performing multiplication on " + operand1 + " and " + operand2);
                calcStack.push(tmp);
            }
            else if(j.compareTo("/") == 0){
                tmp = operand1 / operand2;
                System.out.println("Performing division on " + operand1 + " and " + operand2);
                calcStack.push(tmp);
            }
            else if(j.compareTo("-") == 0){
                tmp = operand1 - operand2;
                System.out.println("Performing subtraction on " + operand1 + " and " + operand2);
                calcStack.push(tmp);
            }
            else if(j.compareTo("+") == 0){
                tmp = operand1 + operand2;
                System.out.println("Performing addition on " + operand1 + " and " + operand2);
                calcStack.push(tmp);
            }
            System.out.println("Pushing result to stack: " + tmp);

        }
        else {

            double newOperand = new Double(j);
            System.out.println("Pushing operand to stack: " + newOperand);
            calcStack.push(newOperand);
        }
    }
    return calcStack.pop();
}