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 / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

Checking for Arguments

Thread view: 
Thrillhouse - 18 Feb 2005 03:52 GMT
I'm writing a program that converts back and forth between different
representations of bits (e.g. One's Complement, Two's Complement, Signed
Magnitude, etc.).  The command for executing the program is "java conv1s
-d 000000" where 000000 indicates the bit pattern to be converted and "-d"
indicates the form to be converted to, in this case decimal.  If there is
no bit pattern then I still want the program to run but with random
numbers.   However, when I check to see if there is no bit pattern I say
"if(args[1] == null)" and that has been throwing an array out of bounds
exception.  Anybody know any other way of checking to see if there is no
bit pattern?
Thanks for your help.
SMC - 18 Feb 2005 05:42 GMT
> I'm writing a program that converts back and forth between different
> representations of bits (e.g. One's Complement, Two's Complement, Signed
[quoted text clipped - 7 lines]
> if there is no bit pattern?
> Thanks for your help.

if (args.length <= 1)

Signature

Sean

I've always found [the news media] quite accurate, except when they're
reporting on a topic I know something about. --usenet post

Anthony Borla - 18 Feb 2005 05:52 GMT
> I'm writing a program that converts back and forth between
> different representations of bits (e.g. One's Complement,
[quoted text clipped - 8 lines]
> any other way of checking to see if there is no bit pattern?
> Thanks for your help.

You should check for the number of command line arguments *before*
attempting to access the command line arguments array. This is important
since, if *no* arguments are passed, the array will have zero elements:
hence, any element-access attempt will fail.

Therefore, you might do something along these lines:

   ...
   public static void main(String[] args)
   {
        if (args.length != 2)
        {
             // Don't have two arguments, so assume defaults, or
             // issue error mesage, whatever ...
        }
        else
        {
            // Got two arguments, do other checking, array access,
            // whatever ...
        }
        ...
   }
   ...

I hope this helps.

Anthony Borla


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.