• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

check if a string is in a hashmap

 
Dan Rh Jones
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afternoon

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?

Cheers!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read the documentation for String#split() and see whether any element in its output can be null.
And remember this:- "" is not null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic