So I am trying to use HashMaps by specififying:
HashMap totalAtt = new HashMap<String, Integer>();
But when I try to add two of the Integers, it gives me an error saying bad operand. How can I add the integers I retrieve from this HashMap without a compiler error or warning?
Edit: Replaced some code, no longer getting compiler error, rather warning of unchecked or unsafe operations
public HashMap<String, Integer> getAttrib()
{
HashMap<String, Integer> totalAtt = new HashMap();
//Creates key values and initializes them to 0
totalAtt.put("strength", 0);
totalAtt.put("dexterity", 0);
totalAtt.put("constitution", 0);
totalAtt.put("intelligence", 0);
totalAtt.put("wisdom", 0);
totalAtt.put("charisma", 0);
HashMap<String, Integer> sAtt;
for(Sprite s: itemList)
{
//iterates through items and counts their attributes
sAtt = s.getAttrib();
totalAtt.put("strength", totalAtt.get("strength") + sAtt.get("strength"));
totalAtt.put("dexterity", totalAtt.get("dexterity") + sAtt.get("dexterity"));
totalAtt.put("constitution", totalAtt.get("constitution") + sAtt.get("constitution"));
totalAtt.put("intelligence", totalAtt.get("intelligence") + sAtt.get("intelligence"));
totalAtt.put("wisdom", totalAtt.get("wisdom") + sAtt.get("wisdom"));
totalAtt.put("charisma", totalAtt.get("charisma") + sAtt.get("charisma"));
}
return totalAtt;
}
From Sprite class:
public HashMap<String, Integer> getAttrib()
{
return attrib;
}
HashMapreference variable is probably not a good idea.HashMap totalAtt = new HashMap<String, Integer>();withHashMap<String, Integer> totalAtt = new HashMap<>();.