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
Joined: Sep 07, 2010
Posts: 98
posted
0
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:
[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]
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
Joined: Jan 28, 2012
Posts: 34
posted
0
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
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?
luck, db
There are no new questions, but there may be new answers.
shirish kulkarni
Greenhorn
Joined: Oct 06, 2012
Posts: 1
posted
0
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