Read File into Hashmap ArrayIndexOutOfBounds Exception

I'm trying to get a file read into a HashMap and I keep getting ArrayIndexOutOfBounds Exception. This is a part of the file I'm reading in:

Java is unicode based so there shouldn't be any problem with this right? I'm new to using HashMaps and I/O

Stephan van Hulst

Saloon Keeper

Posts: 7987

143

posted 3 years ago

1

Hi Braden, welcome to CodeRanch!

Java knows about Unicode, but does your FileReader? FileReader uses your system's default encoding, which is generally not Unicode. Anyway, I suppose the problem can be caused by a multitude of things, such as having blank lines in your input file. You can solve a lot of problems by using a Scanner.

Don't use try-catch blocks like that. you should use try-with-resources, do all the reading inside it, and catch specific exceptions, never Throwable, Error, Exception or RuntimeException.

Don't use meaningless abbreviations like "hm". Instead, use a name such as "romaji".

Let's put it all in an example:

In this example, your file is automatically closed for you (because of try-with-resources), and you can specify the character set with InputStreamReader's constructor.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.

Braden Starcher

Greenhorn

Posts: 3

posted 3 years ago

Stephan van Hulst wrote:Hi Braden, welcome to CodeRanch!

Java knows about Unicode, but does your FileReader? FileReader uses your system's default encoding, which is generally not Unicode. Anyway, I suppose the problem can be caused by a multitude of things, such as having blank lines in your input file. You can solve a lot of problems by using a Scanner.

Don't use try-catch blocks like that. You should use try-with-resources, do all the reading inside it, and catch specific exceptions, never Throwable, Error, Exception or RuntimeException.

Don't use meaningless abbreviations like "hm". Instead, use a name such as "romaji".

Let's put it all in an example:

In this example, your file is automatically closed for you (because of try-with-resources), and you can specify the character set with InputStreamReader's constructor.

That code is excellent, man, thank you. I changed " " to "" and it worked though, but I think I'm going try and finish Introduction to Java FileIO and return, I don't think I know enough Java to complete this project properly.

Stephan van Hulst

Saloon Keeper

Posts: 7987

143

posted 3 years ago

Keep working at it! I/O can get very tricky, especially when you get to the realm of outputting characters in one program, and viewing them in another, with a specific font.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.

Braden Starcher wrote:That code is excellent, man, thank you. I changed " " to "" and it worked though, but I think I'm going try and finish Introduction to Java FileIO and return, I don't think I know enough Java to complete this project properly.

Not to downgrade Stephan's example at all, but there are other ways of dealing with "fields" in lines of text.

Personally, I'm not a big fan of Scanner, so I would probably use String.split(), viz:but then you might want to catch ArrayIndexOutOfBoundsException as well, to handle invalid lines.

Which just goes to show that, even among experienced bods, there are many ways to do things.

PS: That "UTF-8" constructor is a very good one to remember, since it will also work with standard 7-bit ASCII; so you can sometimes use it as a 'catch-all'.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here

Stephan van Hulst

Saloon Keeper

Posts: 7987

143

posted 3 years ago

And to add to the "many ways" argument, I remember I once did this with a Pattern with catching groups. Add a Scanner to the mix (sorry Winston) and you can have a pretty succinct piece of code:
No exceptions, no readLines, it takes extra whitespace and malformed lines into account, and it also checks whether the scripts match.

It comes at a price though. Regular expressions get very messy quickly, and they are difficult for beginners to learn.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.

Stephan van Hulst wrote:And to add to the "many ways" argument, I remember I once did this with a Pattern with catching groups. Add a Scanner to the mix (sorry Winston) and you can have a pretty succinct piece of code...

No sorry about it. I'm quite happy to admit that my dislike for Scanner is probably due to the fact that I haven't studied it enough (although, in my defence, I'd ask why a class that was supposed to make things simpler needs to be studied so much). And I do like your last example.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here

Stephan van Hulst

Saloon Keeper

Posts: 7987

143

posted 3 years ago

1

I would argue that lots of things that are designed to make our lives easier need to be studied a lot ;)

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.

Braden Starcher

Greenhorn

Posts: 3

posted 3 years ago

I'm actually having the same problem again, so I'll post the whole code. It's still java.lang.ArrayIndexOutOfBoundsException: 1 at HiriganaTest.<init>(HiriganaTest.java:133), and the console is also printing ��a on the very last line. As you can see I have tried Stephan's method, although it is commented out, it just returned the same error. Perhaps there is something else wrong that I haven't noticed or don't know that it is wrong. Any help would be vastly appreciated again.