Adding values in hashtable
Posted on 2006-04-04
First, I'd like to say that I feel like a bit of a fool tonight... This is the third question I've asked. The beauty is, that they're all different programs I've been working on, but have resolved to complete them all tonight after a few niggling problems I've had. After this one, I only have one more and that doesn't look to be very problematic so I should be ok!
Either way, another 500 points are up for grabs in what seems to be a very easy question...
I have a hashtable with several values
Hashtable time = new Hashtable();
time.put(1, new Integer(0));
time.put(2, new Integer(180));
time.put(3, new Integer(210));
time.put(4, new Integer(180));
time.put(5, new Integer(240));
time.put(6, new Integer(240));
time.put(7, new Integer(210));
time.put(8, new Integer(90));
time.put(9, new Integer(120));
time.put(10, new Integer(85));
Let's say that I want to add up the values of keys 3-7 (210+180+240+240+210).
int total = 0;
for (int i = 3; i<7;i++)
total = total + time.get(i);
I get a problem where the error is "operator + cannot be applied to int,java.lang.Object."
But I've clearly cast the value as an integer.
Am I missing something completely obvious?!
Thanks in advance.