I am trying to add up all the values for any Hashmap. The hash map will have two Integers. First as a key and the second as a value. I am getting errors in the compiler as I am not sure how to do this. I want to return the total value of all the values in the hash
map.
I would like this method to work for any hash
map that I place into this method's parameter and return
I am a 7th grader trying to learn java on my own at the moment. If someone can show me how to also run this in a main method with a passed in hash map that would be super.
I am getting these errors
:
Multiple markers at this line
- value cannot be resolved to a type
- Type mismatch: cannot convert from element type Integer to value
- Line breakpoint:PopulationTotal [line: 10] - getTotal(HashMap)
- Syntax error, insert "Identifier" to complete
EnhancedForStatementHeaderInit
I am trying this:
public int getTotal (HashMap<Integer, Integer> p) {
for (value : p.values()) {
int total += value;
}
return total
}