Solved

Posted on 2006-04-04
Medium Priority
210 Views
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?!

0
Question by:DanBAtkinson
• 3
• 3
• 3

LVL 92

Accepted Solution

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

LVL 15

Expert Comment

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

ID: 16376372
Thanks!

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

LVL 15

Expert Comment

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

ID: 16376397
Which values are you referring to?
0

LVL 15

Expert Comment

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

Author Comment

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

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

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

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