0

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;
}
4
  • 3
    Please include more of your code that is involved in reproducing your error, along with the line that produces the error, and the error itself. BTW, the raw HashMap reference variable is probably not a good idea. Commented Apr 15, 2016 at 23:18
  • 3
    Maybe just replace HashMap totalAtt = new HashMap<String, Integer>(); with HashMap<String, Integer> totalAtt = new HashMap<>();. Commented Apr 15, 2016 at 23:22
  • @rgettman Added more code, the current error is coming from: totalAtt.put("strength", totalAtt.get("strength") + sAtt.get("strength")); Commented Apr 15, 2016 at 23:22
  • @DavidS that removes the compiler error, but it's still giving me a compiler warning: "....uses unchecked or unsafe operations." Commented Apr 15, 2016 at 23:24

1 Answer 1

3

Change

HashMap totalAtt = new HashMap<String, Integer>();

to

HashMap<String, Integer> totalAtt = new HashMap<>();

and

HashMap sAtt

to

HashMap<String, Integer> sAtt
Sign up to request clarification or add additional context in comments.

5 Comments

But if a comment is THE answer to the question it should be made an answer.
Besides that I added the second fix. ;-)
Okay I updated some code with the <String, Integer> in the proper place, still getting compiler error however
Which one do you get?
The <> at the end of new HashMap<> worked! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.