Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Array out of bounds exception .split() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array out of bounds exception .split()" Watch "Array out of bounds exception .split()" New topic

Array out of bounds exception .split()

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 101
Hi all.

I don't get this, when i use string.split() on an array declared as having two values, i get the OutOfBoundsException:

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Couple of things

You're "reading" file wrong. Read an IO tutorial such as this one.
You're not closing your resources properly.

The array might not have a size of 2 because it gets reassigned on line 9. Try printing the size of it before you use it.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
N Sahni
Ranch Hand

Joined: Jul 07, 2011
Posts: 55

At line 7 reader.toString() will invoke java Object's toString() and it won't give you file contents.
Hence, s.split("/") will always give you zero length string.

You need to go through the tutorial mentioned by Wouter and read file properly.

Thanks and Regards,
Nilesh Sahni | |
I agree. Here's the link:
subject: Array out of bounds exception .split()
It's not a secret anymore!