• Post Reply Bookmark Topic Watch Topic
  • New Topic

arrayoutofboundserror from passing an argument.  RSS feed

 
Daniel Hyslop
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just returned to java and coding after being sometime away from it. I have bought a book " Java in easy steps" and have come across a problem where the code I`m asked to enter compiles ok
but when ran it throws an arrayoutoffbounds exception error. I know it`s not me as I`ve cut and pasted the code from the books web site and I get the same error from that code.
The chapter this code comes from is " Passing an argument". Can anyone see what I`m missing,
Please.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the Java™ Tutorials section about the platform? Look particularly at the bit about command line arguments. If you have to use args[0], then args must have ≥ 1 element in. That means you must pass a value when you run the class. Not
java Option
but …
java Option -es
Then "-es" will constitute args[0]. If you don't pass that information then args will have 0 length and you cannot even find element 0.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when ran it throws an arrayoutoffbounds exception error.
There is no such a exception error as "arrayoutoffbounds". There is "ArrayIndexOutOfBoundsException".
Java is case sensitive language, so it is important to spell class, variables, method names correctly, otherwise you'd run into problems.

Please show us, how you executed your program?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: how old is that book? Try replacing the ifs with a switchOnly works in Java7+
 
Daniel Hyslop
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys . Used Campbell's solution and it now works fine. Didn`t realize the argument had to be passed on the command line. The book is the latest 5th edition in "Java easy steps" series of books.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 5th edition is pretty recent; you might find Strings in switch statements in it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!