乘法时出现 NumberFormatException

NumberFormatException while multiplying

我正在尝试 'Run Configurations' 在 Eclipse 中。 当我传递诸如“1 + 2”、“123 - 321”或“123 / 321”之类的东西时,效果很好。 当我尝试乘法时出现问题。在这种情况下,我得到

Exception in thread "main" java.lang.NumberFormatException: For input string: ".project"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at Main.main(Main.java:15)

代码如下:

public class Main {

public static void main(String[] args) {
    double aNum;
    double bNum;
    char operator;
    String result = "Error";
    Calculator calc = new Calculator();

    if (args.length == 0) {
        System.out.println("No parameters were entered");
    }
    else {
        aNum = Double.parseDouble(args[0]);
        bNum = Double.parseDouble(args[2]);
        operator = args[1].charAt(0);
        result = calc.calculate(aNum, bNum, operator);

        System.out.println(result);
    }
}

}


public class Calculator {
public String calculate(double aNum, double bNum, double operator) {
    String result = "Error";
    if(operator=='+'){
        result = String.valueOf(aNum+bNum);
    }
    else if (operator=='-') {
        result = String.valueOf(aNum-bNum);
    }
    else if (operator=='*') {
        result = String.valueOf(aNum*bNum);
    }
    else if (operator=='/') {
        if (bNum==0) {
            System.out.println("Forbidden operation: div by zero!");
        }
        else {
            result = String.valueOf(aNum/bNum);
        }
    }
    else {
        System.out.println("Unhandled operator. Please use '+-*/' as operators");
        result = "Error";
    }
    return result;
}
}

问题在于您调用程序的方式。如果你 运行:

java Calculator 5 * 10

然后在某些命令外壳中,* 将自动扩展为当前目录中的所有文件名。您应该可以通过引用来解决这个问题,例如

java Calculator 5 '*' 10

或者从计算器中获取值,而不是从命令行中获取。