java - Cannot find associated value in a hashmap from a passed in key -
i working existing code in eclipse project. in method below called cardtypeforpbfvalue()
, cannot find key in hashmap
though can see while debugging code. pbfvaluemap
populated follows:
[1=atm, 2=debit, 3=credit, 4=payroll]
i'm not sure why cannot associated value of credit
when i'm passing in value of 3 in cardtypeforpbfvalue()
below. getting value of null
.
any help/direction appreciated.
here code i'm working with:
public static enum cardtype { credit(3), atm(1), debit(2), payroll(4); cardtype(int pbfvalue) { this.pbfvalue = (short) pbfvalue; } public static hashmap<short, cardtype> pbfvaluemap = new hashmap<short, cardtype>(); static { (cardtype cardtype : cardtype.values()) { short value = cardtype.pbfvalue; pbfvaluemap.put(cardtype.pbfvalue, cardtype); } } public static cardtype **cardtypeforpbfvalue**(int pbfvalue) { cardtype returnvalue = pbfvaluemap.get(pbfvalue); if (returnvalue == null) { returnvalue = debit; } return returnvalue; } public short pbfvalue; }
you're looking integer
, put short
map. try this:
public static cardtype cardtypeforpbfvalue(int pbfvalue) { short shortpbfvalue = (short) pdbvalue; cardtype returnvalue = pbfvaluemap.get(shortpbfvalue); ... }
better yet, stop using int
everywhere (or stop using short
map) - consistent in type want use.
Comments
Post a Comment