The problem is trivial, but I am missing some very basic stuff here and unable to catch it. Please help. I am writing a simple calculator program to work with at command line. The source code is given below. The problem is when I use the calculator as
>java SwitchCalc 12 * 5
it throws a 'java.lang.NumberFormatException' for input string: "002.java" in the statement parsing 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?
/*
User will input the expression from command-line in the form:
>java SwitchCalc value1 op value2
where,
value1, and value2 are integer values
op is an operator in +, -, *, /, %
Program will evaluate the expression and will print the result. For eg.
>java SwitchCalc 13 % 5
3
*/
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);
//System.out.println(value1 + " " + args[1] + " " + value2 + " = " + result);
}
}
002.java
, in the directory from which you are running this code. – Elli