wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
Author

check if a string is in a hashmap

Dan Rh Jones
Greenhorn

Joined: Aug 16, 2011
Posts: 19
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

Joined: Oct 02, 2003
Posts: 11153
    
  16

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
Sheriff

Joined: Oct 13, 2005
Posts: 37939
    
  22
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: http://aspose.com/file-tools
 
subject: check if a string is in a hashmap
 
Similar Threads
How do i improve variable visibility?
using hashtable as return type
HashMap Initialization
HashMap Question
Need help quickly....intelligent city name problem