?
Solved

Adding values in hashtable

Posted on 2006-04-04
9
Medium Priority
?
210 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:DanBAtkinson
  • 3
  • 3
  • 3
9 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 16376296
   total = total + ((Integer)time.get(i)).intValue();
0
 
LVL 15

Expert Comment

by:Javatm
ID: 16376370
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
 

Author Comment

by:DanBAtkinson
ID: 16376372
Thanks!

Do I need to put "new Integer(xx)" around the values then?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 15

Expert Comment

by:Javatm
ID: 16376389
> 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
 
LVL 92

Expert Comment

by:objects
ID: 16376397
Which values are you referring to?
0
 
LVL 15

Expert Comment

by:Javatm
ID: 16376410
Your fast ;-D I was trying to make a code out if it.
0
 

Author Comment

by:DanBAtkinson
ID: 16376444
@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
 

Author Comment

by:DanBAtkinson
ID: 16376465
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
 
LVL 92

Expert Comment

by:objects
ID: 16376471
>      Hashtable time = new Hashtable();

to remove need for casting you could use:

     Hashtable<Integer, Integer> time = new Hashtable<Integer, Integer>();
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question