The problem is trivial, but here I missed some very simple things and could not catch it. Please help. I am writing a simple calculator program to work on the command line. The source code is given below. The problem is that I use the calculator as
>java SwitchCalc 12 * 5
it throws "java.lang.NumberFormatException" for the input line: "002.java" in the expression expressing the second int from args [2]:
int value2 = Integer.parseInt(args[2])
Later I tried the following, it worked.
>java SwitchCalc 12 "*" 5 12 * 5 = 60
What am I missing?
class SwitchCalc{ public static void main(String [] args){ int value1 = Integer.parseInt(args[0]), value2 = Integer.parseInt(args[2]), result = 0; switch(args[1]){ case "+": result = value1 + value2; break; case "-": result = value1 - value2; break; case "*": result = value1 * value2; break; case "/": result = value1 / value2; break; case "%": result = value1 % value2; break; default: System.out.printf("ERROR: Illegal operator %s.", args[1]); break; } System.out.printf("%d %s %d = %d", value1, args[1], value2, result);
source share