Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Help! Command-line arguments

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13014
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic