File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Command line arguments help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Command line arguments help." Watch "Command line arguments help." New topic
Author

Command line arguments help.

David Maddox
Greenhorn

Joined: Feb 25, 2004
Posts: 12
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
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

Joined: Oct 08, 2010
Posts: 131

[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

Joined: Nov 02, 2011
Posts: 34

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

Joined: Jan 28, 2012
Posts: 34
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

Joined: May 03, 2008
Posts: 4523
    
    5

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
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



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Command line arguments help.
 
Similar Threads
TicTacToe Game
TicTacToe in Java Applet
TicTacToe Game..
TicTacToe game
TicTacToe - Computer's move