It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Treemap: Problem doing put(int, double) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Treemap: Problem doing put(int, double)" Watch "Treemap: Problem doing put(int, double)" New topic
Author

Treemap: Problem doing put(int, double)

James Gallagher
Greenhorn

Joined: Jul 09, 2003
Posts: 1
I'm having a problem that I just can't see through at the moment. Been searching Google for an example but just not getting anywhere. Here's a (hopefully relevant & complete) snippet of code that I'm using:
java.util.TreeMap inputParms = new java.util.TreeMap();
Double myDouble = new Double(someString);
inputParms.put(new Integer(1), myString);
inputParms.put(new Integer(2), myDouble);
Basically, the TreeMap is to hold a set of parameters that are used elsewhere (they are the inputs used to call a stored procedure - that part is written by someone else, I just populate the TreeMap with the appropriate types and values).
My problem is this throws an exception: java.lang.Double. If I use anything other than a Double object then I don't have a problem (I've tried String, Long and java.sql.Date, though not Float - just occurred to me now that I'd missed that). I tried using myDouble.doubleValue() and this throws an undefined symbol for the method put(int, double). I'm certain that someString can be converted to a Double without problems - have verified this several times. If I do Long myLong = new Long(someString), I don't have a problem. However Long isn't sufficient for my requirements.
If somebody could show me a working example of putting a Double/double into a TreeMap or pointing to me a solid example I would appreciate it.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi James,
Welcome to JavaRanch. What is the exact Exception being thrown?


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
As Michael said, whats the Exception?
and as for a working sample, heres your own code back and it works fine.
I've just added the declaration/init of myString and someString.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I tried using myDouble.doubleValue() and this throws an undefined symbol for the method put(int, double). I'm certain that someString can be converted to a Double without problems
You can only put objects in a TreeMap. myDouble.doubleValue() is a primitive double, not an object. This isn't to do with converting a String to a Double.
In Java 1.5, with autoboxing, you will be able to code map.put(int, double), and the compiler will box up (using the Integer and Double wrapper classes) the primitives automatically for you.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Treemap: Problem doing put(int, double)