File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes ArrayIndexOutOfBoundException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayIndexOutOfBoundException" Watch "ArrayIndexOutOfBoundException" New topic


Bill Boyd

Joined: Dec 30, 2002
Posts: 4
Greetings fellow Javaranch readers,

I am trying to learn from one of the HeadFirst java books
(Second Edition). The book has been very good except for some examples that I cannot get to work correctly. In chapter 16 the topic
is to learn about generic classes and arrays as used in java 5.0.
(I understand and can compile this concept correctly in 1.42.) The
example is working correctly, but when I try to use an array element that is not the first one I get an array out of bounds error. Is there something that should be done differently? In the next example I also get the same kind of error "java.lang.ArrayIndexOutOfBoundException: 1". Although this is not the lesson for this chapter I do not think I should be getting this error. Below I have posted the code that give me the error. Please let me know if anyone has any idea of what I am encountering.

Any Help would be appreciated Thanks,

Bill Boyd (Edited by Bill Boyd 1:32 Saturday)
[ January 27, 2007: Message edited by: Bill Boyd ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Funny. When I put in a missing closing "}" for the while loop, I get the following output:

No problem - is there something wring with your datafile?

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Henry Wong

Joined: Sep 28, 2004
Posts: 20525

No problem - is there something wring with your datafile?

Agreed. Two things to look for. Any extra lines -- especially blank lines. These lines don't have any "/" delimiters, hence, they will only return an array of size one.

The other thing is any lines where the second field is blank. The default split() method, do not return any trailing zero length fields.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bill Boyd

Joined: Dec 30, 2002
Posts: 4
Dear Barry,
Thank you for your reply. I edited the curly brace out of my first posting by mistake. After I put the missing curly brace back into the code it ran just fine. I still got the same error, with this example and the code example in the HeadFirst book after it too. The next example had a text file that I also made from scratch. I made an entirely new data file and did not get the error that I had earlier. I must have had an issue with the way the text file saved. Again thanks for the reply.

Bill Boyd
"Never Underestimate the Stupidity of the General Public"
I agree. Here's the link:
subject: ArrayIndexOutOfBoundException
It's not a secret anymore!