• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing an arguement  RSS feed

 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this short code,


which works... mostly, except when I get to the else statement.

I type 'java option -en' and I get English option,

I type 'java option -es' I get the Spanish option, so far so good.

I type 'java option' I get " Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at option.main(option.java:5) "

but when I type 'java option q' I get "unrecognised Option"

So my question is do I have to use a Scanner scan = new Scanner(System.in) and nested if statements to make this program work properly, or is there a .... more elegant solution?

(Solved) I did not read the question correctly, working as intended.

 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you type in no arguments at all the length of the args array is zero and when you try to get the String at args[0] you are out of bounds. It is best if the first check you make is

and handle that appropriately before any other args comparison.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!