This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help! Command-line arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help! Command-line arguments" Watch "Help! Command-line arguments" New topic
Author

Help! Command-line arguments

Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
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
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
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
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11257
    
  16

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!!!


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help! Command-line arguments