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
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
From the JCreator main menu, select Configure Options JDK Tools Select Tool Type (select "Run Application") click on <default> (buttons now enabled) click Edit button select Parameters tab check the box for "Prompt for main method arguments" OK all the way out now run the program, and when the dialog box appears, enter + 63 40 (note the spacing) these are the 3 arguments required by the program to run
Stacey Johnson
Ranch Hand
Joined: Jan 11, 2004
Posts: 55
posted
0
Thank you soo much, now I can go and tackle the two programs I have to write using command line parameters. I'm sure you'll see me in the next few days with questions. But thanks for the initial understanding anyways! Stacey
this is a good example of how IDEs (like JCreator and JBuilder) can make life more difficult, especially when learning the language. You now have to spend time learning the ins and outs of the ide in addition to trying to learn the language. You really should take the time to learn how to compile and run from the command line. Yes, it can be a major pain, and a struggle, and you'll wonder why your doing it, but in the end it helps you understand everything better. just my 2 cents... congrats on getting it to work!!!
Never ascribe to malice that which can be adequately explained by stupidity.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.