File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes check if a string is in a hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "check if a string is in a hashmap" Watch "check if a string is in a hashmap" New topic

check if a string is in a hashmap

Dan Rh Jones

Joined: Aug 16, 2011
Posts: 19

I am a little stuck on something.

The code below is from something I am writing for the Java Ranch. OOP-2 NaturalLanguageMultiply

Given a string, I need to be able to check if that string is in my hashmap and if not then throw an exception

This works fine and dandy if I have a string that creates two elements in the array (String number = "two-three";) but if it only has one, (String number = "three";), I get a ArrayIndexOutOfBoundsException

I am stuck! Any pointers on how to do this?

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

if you're string contains "two", and you call split() on it with "-" as your token to split on, what are you expecting to find in pieces? How many elements do you think will be in that array?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Please read the documentation for String#split() and see whether any element in its output can be null.
And remember this:- "" is not null.
I agree. Here's the link:
subject: check if a string is in a hashmap
It's not a secret anymore!