Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Treemap: Problem doing put(int, double)

 
James Gallagher
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,
Welcome to JavaRanch. What is the exact Exception being thrown?
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic