NumberFormatException while multiplying

I'm trying to 'Run Configurations' in Eclipse. When I pass something like '1 + 2', or '123 - 321', or '123 / 321' it works well. The problem appears when I try to do multiplying. In this case I get

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)

Here's the code:

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;
}
}
Jon Skeet
people
quotationmark

The problem is how you're invoking the program. If you run:

java Calculator 5 * 10

then in some command shells, the * will be automatically expanded to all filenames in the current directory. You should be able to fix this with quoting, e.g.

java Calculator 5 '*' 10

Or ask for the values from within the calculator, instead of taking them from the command line.

people

See more on this question at Stackoverflow