Hi Everyone,
I was wondering if I could get some insite on "Command-line arguments". The text book I'm reading explains it a bit, but I don't fully understand it. The code it gives is as follows:
// Calculator.java: Pass parameters from the command line
public class Calculator {
/** Main method */
public static void main(
String[] args) {
// Check command-line arguments
if (args.length != 3) {
System.out.println(
"Usage:
java Calculator operator operand1 operand2");
System.exit(0);
}
// The result of the operation
int result = 0;
// Determine the operator
switch (args[0].charAt(0)) {
case '+': result = Integer.parseInt(args[1]) +
Integer.parseInt(args[2]);
break;
case '-': result = Integer.parseInt(args[1]) -
Integer.parseInt(args[2]);
break;
case '*': result = Integer.parseInt(args[1]) *
Integer.parseInt(args[2]);
break;
case '/': result = Integer.parseInt(args[1]) /
Integer.parseInt(args[2]);
}
// Display result
System.out.println(args[1] + ' ' + args[0] + ' ' + args[2]
+ " = " + result);
}
}
The output is suppose to be as follows(according to the book):
C:\book>java Calculator
Usage: java Calculator operator operand1 operand2
C:\book>java Calculator + 63 40
63 + 40 = 103
C:\book>java Calculator - 63 40
63 - 40 = 23
C:\book>java Calculator "*" 63 40
63 * 40 = 2520
C:\book>java Calculator / 63 40
63 / 40 = 1
C:\book>
Now when I run the program only the first two lines print. I'm using JCreator. I can't seem to compile my programs through DOS myself. I'm lost, can someone help me??
Stacey