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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
   total = total + ((Integer)time.get(i)).intValue();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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);
      }
}
DanBAtkinsonAuthor Commented:
Thanks!

Do I need to put "new Integer(xx)" around the values then?
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

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 ;)
Mick BarryJava DeveloperCommented:
Which values are you referring to?
JavatmCommented:
Your fast ;-D I was trying to make a code out if it.
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.
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!
Mick BarryJava DeveloperCommented:
>      Hashtable time = new Hashtable();

to remove need for casting you could use:

     Hashtable<Integer, Integer> time = new Hashtable<Integer, Integer>();
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.