• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Command line arguments help.

 
David Maddox
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never used command line arguments before, so I am having a little difficulty.

Essentially what I am trying to do is have this program when there are no arguments, the program runs in a certain mode, and when it has certain arguments to run in different modes:

No arguments: Human v Human
-c: Computer v Computer
-c 1 Computer is player 1
-c 2 Computer is player 2


When I try and run the program in its current state, I am getting an array index out of bounds exception. Here is the main function:



Thank you for your help!
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

If there are no command line arguments, then args[0] is going to be undefined, not null. You need to start by checking args.length and then base your logic on what that value is.

Just to see what is actually getting passed to args, you could temporarily slip something like this into your code:


 
rohit chavan
Ranch Hand
Posts: 132
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[size=12]you would be running your program like "java YourClass" on command prompt, then args.length would be zero so check for the same.
yuo need to change your if condition like, [/size]
 
Kiran Yadav
Ranch Hand
Posts: 35
Eclipse IDE Firefox Browser Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more point regarding Command line arguments,

If Command line arguments are not provided then empty String array is created. If we try to access the arguments of this String array or try to modify any argument then array index out of bound exception exception will occur.

e.g. The following code is from one of the SCJP question


Case 1: No command line arguments are provided

In this case, assigning null value to String array reference like will work fine, but if any argument is initialized like then array index out of bound exception will occur, this is because we are initializing the arguments of empty array.

Case 2: Command line arguments are provided

In this case, if we provided command line arguments, then the no. of arguments which are provided, those many arguments can be only re-initialized
e.g. If command line arguments = 2
"Arg1" "Arg2"

Then following code will work,
args[0] = "test";
args[1] = "test";

The out will be the overriden values

If no. arguments provided are less than re-initialized size then also index out of bound exception will occur.



 
Viktor Kubinec
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kiran Yadav wrote:
e.g. The following code is from one of the SCJP question



This will not compile, because you cannot catch NullPointerException after Exception.

Also this can be usefull for building command line applications:
Apache CLI
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Viktor Kubinec wrote:Also this can be usefull for building command line applications:
Apache CLI


Again, as I asked you in another thread, why would you want to use a third party library for something that can so easily be done using the JDK classes?
 
shirish kulkarni
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kiran Yadav wrote:One more point regarding Command line arguments,

If Command line arguments are not provided then empty String array is created. If we try to access the arguments of this String array or try to modify any argument then array index out of bound exception exception will occur.

e.g. The following code is from one of the SCJP question


Case 1: No command line arguments are provided

In this case, assigning null value to String array reference like will work fine, but if any argument is initialized like then array index out of bound exception will occur, this is because we are initializing the arguments of empty array.

Case 2: Command line arguments are provided

In this case, if we provided command line arguments, then the no. of arguments which are provided, those many arguments can be only re-initialized
e.g. If command line arguments = 2
"Arg1" "Arg2"

Then following code will work,
args[0] = "test";
args[1] = "test";

The out will be the overriden values

If no. arguments provided are less than re-initialized size then also index out of bound exception will occur.
......
but in above code it generates a compiler error..., generic ex in catch block before the specific one



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic