Adding values in hashtable

Hi there.

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.
DanBAtkinsonAsked:
Who is Participating?
 
objectsCommented:
   total = total + ((Integer)time.get(i)).intValue();
0
 
JavatmCommented:
Another way would be :

import java.util.Hashtable;

public class Collection {

      private static int value;
      private static int total;

      @SuppressWarnings("unchecked")
      public static void main(String[] args) {

           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));
          
           for (int i=3; i<8; i++)
           {
               value = Integer.parseInt((String) time.get(i).toString());
               total += value;
           }
           System.out.println(total);
      }
}
0
 
DanBAtkinsonAuthor Commented:
Thanks!

Do I need to put "new Integer(xx)" around the values then?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
JavatmCommented:
> But I've clearly cast the value as an integer.
Casting should be like (Integer) or (String) since you are just passing values of int to a Hashtable Object ;)
0
 
objectsCommented:
Which values are you referring to?
0
 
JavatmCommented:
Your fast ;-D I was trying to make a code out if it.
0
 
DanBAtkinsonAuthor Commented:
@Objects: The values in the hashtable. I've removed them now, so I've answered my own question. I just thought that I could explicitely cast them inside the hashtable instead of having to convert them when i 'got' them.
0
 
DanBAtkinsonAuthor Commented:
I actually have ANOTHER problem leading from this. :( I'd look in my books but I'm not at home with them and I'm simply not expert enough at Java.

If I decide to post this question, that'll be 2000 points I'll have given away this evening!
0
 
objectsCommented:
>      Hashtable time = new Hashtable();

to remove need for casting you could use:

     Hashtable<Integer, Integer> time = new Hashtable<Integer, Integer>();
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.