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