aspose file tools*
The moose likes Beginning Java and the fly likes arrayindexoutofbound when creating hashmap from txt file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arrayindexoutofbound when creating hashmap from txt file" Watch "arrayindexoutofbound when creating hashmap from txt file" New topic
Author

arrayindexoutofbound when creating hashmap from txt file

sam t. yang
Greenhorn

Joined: Feb 23, 2012
Posts: 7

this is the code i have to read a text file that's formatted in the following
1<>CIS 101-1
2<>CIS 101-2
3<>CIS 231-1
4<>CIS 231-2
5<>CIS 234-1
6<>CIS 234-2

when i try to compile my main app, which calls on this method to return in course hashmap, the compiler throws
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at the line


however, on my other java class that has the same code but reads a different text file (with same format) it works perfectly.
can anyone help me figure out what's wrong?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3542
    
  15
sam t. yang wrote:this is the code i have to read a text file that's formatted in the following
1<>CIS 101-1
2<>CIS 101-2
3<>CIS 231-1
4<>CIS 231-2
5<>CIS 234-1
6<>CIS 234-2

on my other java class that has the same code but reads a different text file (with same format) it works perfectly.

Looking at your other post, I would disagree that your text files have the same format.


Joanne
sam t. yang
Greenhorn

Joined: Feb 23, 2012
Posts: 7
my other text was the following
1<>John Kane
2<>Jane Lyman
3<>Peter Hansen
4<>Cathy Harris
5<>Rose Makki
6<>Michael O'Connor

which... i assume was in the same format.
but i'll admit wrong it they are in fact different formats.

I've gotten the above text to work, but not the 1 posted as my question.

Still, could you offer me any help?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3542
    
  15
Sorry. You're right. I was mistaking the numbers at the start for line numbers rather than part of the text.
The error message suggests the String.split call is producing only one String (i.e. no splitting is being done), so I would check your regex - note that it is slightly different to the regex used in your other post ("<>" compared with "\\<\\>").
If you put this code after the call to String.split it will show what is being produced
sam t. yang
Greenhorn

Joined: Feb 23, 2012
Posts: 7
I figured out what was wrong with it. Apparently there was an "empty line" that was invisible in the wordpad, which I have no idea how it got there.

I used


and voila...

it was weird because on my other text file it required me to do no such thing.

thank you for the quick responses though!

/resolved!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: arrayindexoutofbound when creating hashmap from txt file