Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

Java Arguments for main

Thread view: 
dug - 09 Oct 2005 05:27 GMT
Hi all,

If I have a main class called test which has the main method. This
class receives input from the user via the argument list.
as in: java test argument1 argument2 argument3 argument4

Now the issue is that arguments2, arguments3 and argument4 are optional
meaning that the app could receive combinations like:
java test  argument1 argument3 argument4
or
java test  argument1 argument4
or java test  argument1 argument2 argument4
etc.
argument2 to 4 are all numberic so i cannot tell what arguments have
been sent to the app and therefore could result i the app running
incorrectly. Is there a way to "know" which parameters have been sent?
I do not have control over the telling the user to change the arguments
(like using commas) eg:
java test  argument1, , ,argument4

Thanks
Tor Iver Wilhelmsen - 09 Oct 2005 08:18 GMT
> argument2 to 4 are all numberic so i cannot tell what arguments have
> been sent to the app and therefore could result i the app running
> incorrectly. Is there a way to "know" which parameters have been sent?

Not unless you add semantics, either in the form of having different
ranges (e.g. argument2 is in the range 2-8, argument3 in the range
100-200, so if you get the value 145 you know it's for argument3) or
the more common named argument, where you use the names, e.g.

java test argument1=foo argument4=42
Roedy Green - 09 Oct 2005 09:39 GMT
>Is there a way to "know" which parameters have been sent?
>I do not have control over the telling the user to change the arguments
>(like using commas) eg:
>java test  argument1, , ,argument4

Why don't you try the experiment?

The usual way to deal with that is to invent a keyword, e.g.

-height:10  -width:40  -depth:20

Then you can put them in any order or leave some out and use a
default.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Mike Schilling - 09 Oct 2005 21:22 GMT
> Hi all,
>
[quoted text clipped - 12 lines]
> been sent to the app and therefore could result i the app running
> incorrectly. Is there a way to "know" which parameters have been sent?

Not from what you've told us, no.

> I do not have control over the telling the user to change the arguments
> (like using commas) eg:
> java test  argument1, , ,argument4

Then you need to go to whoever specified this, and say "It's ambiguous as
specified.  Fix it."
"." - 12 Oct 2005 17:40 GMT
> Hi all,
>
[quoted text clipped - 15 lines]
> (like using commas) eg:
> java test  argument1, , ,argument4

You either have to always input four arguments or have some sort of way to
identify the arguments. Most programs use switches. For example,

    java test -arg3 1 -arg1 87 -arg4 99999

I then know that if args[i].equals("-arg1") then args[i+1] is the value of
the first argument. The other way is to do something like:

    java test 87 "" 1 99999

The order is important and missing arguments are represented by the "". So
in the example argument 1 is 87, argument 2 is 'missing', argument 3 is 1
and argument 4 is 99999.

If you go for the first option you also might want to consider how things
like:

    java test -arg187 -arg499999 -arg31

might be handled.

Signature

Send e-mail to: darrell dot grainger at utoronto dot ca

Gordon Beaton - 13 Oct 2005 07:56 GMT
> You either have to always input four arguments or have some sort of
> way to identify the arguments. Most programs use switches. For
[quoted text clipped - 4 lines]
> I then know that if args[i].equals("-arg1") then args[i+1] is the
> value of the first argument.

Rather than implement this (and the necessary error checking)
yourself, check out Java Getopt, which provides functionality
identical to that of getopt() in C:

 http://www.gnu.org/software/java/packages.html

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.