Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrayindexoutofbound when creating hashmap from txt file

 
sam t. yang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sam t. yang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic