File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBoundException

 
Bill Boyd
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
Bill Boyd
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic