Here is the code
private static HashMap naturalNumbers = new HashMap();
static
{
naturalNumbers.put("zero", new Integer( 0 ) );
naturalNumbers.put("one", new Integer( 1 ) );
naturalNumbers.put("two", new Integer( 2 ) );
naturalNumbers.put("three", new Integer( 3 ) );
}
private static int findANumber( String partOfaNumber ) throws Exception
{
int multiplicand = 0;
multiplicand += (Integer)naturalNumbers.get( partOfaNumber );
If the “get” returns null, how do I check for this?
I have tried:
if ( (Integer)naturalNumbers == null )
{
throw new Exception( "Number not found" );
}
return multiplicand;
}
but the IDE does not even accept it: cannot convert from HashMap to Integer.
Here’s a slightly different version: