这是代码
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 );
如果“get”返回 null,我该如何检查?
我试过了:
if ( (Integer)naturalNumbers == null )
{
throw new Exception( "Number not found" );
}
return multiplicand;
}
但 IDE 甚至不接受它:无法从 HashMap 转换为 Integer。