This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Command line arguments help.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Ranch Hand
Posts: 133
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 35
Eclipse IDE Firefox Browser Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
He's my best friend. Not yours. Mine. You can have this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic