3 Replies - 6034 Views - Last Post: 09 September 2010 - 01:11 AM Rate Topic: -----

#1 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

Convert Object to type double?

Posted 08 September 2010 - 11:40 PM

Hello there!

Just a straight question: How do I convert an object to type double?

*I could of googled this but all the sites that look like they have the
answer gets blocked by my work...the bastards.

Thanks guys!
Is This A Good Question/Topic? 0
  • +

Replies To: Convert Object to type double?

#2 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Convert Object to type double?

Posted 08 September 2010 - 11:56 PM

If the object is not a Double then we would have to know what it is. Otherwise, if it is a Double and is simply being referenced as an Object (i.e. as when retreived from the Session object as per your other threads) then simply cast it.
Was This Post Helpful? 1
  • +
  • -

#3 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

Re: Convert Object to type double?

Posted 09 September 2010 - 12:31 AM

View Postmasijade, on 08 September 2010 - 10:56 PM, said:

If the object is not a Double then we would have to know what it is. Otherwise, if it is a Double and is simply being referenced as an Object (i.e. as when retreived from the Session object as per your other threads) then simply cast it.


Yeah it's a Double sessionAttribute that I now have to get and convert to a double so that it can work in the System.out.print() method. Since Double doesn't work in System.out.print().

So I can just do this?:
Double dObj = new Double("10.50");
double d = dObj.doubleValue();
System.out.println(d);



Another thing that I'm looking into now, that I can't wrap my mind around (thought I might as well ask you): I have a JSP with a HTML-form in it. The form information gets sent to a servlet where it sets a sessionAttribute. Then, I need that sessionAttribute to go back to the JSP. So I need to know how to forward stuff BACK to the JSP?

Thanks masijade, you have no idea how much you helping me here..
Was This Post Helpful? 0
  • +
  • -

#4 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Convert Object to type double?

Posted 09 September 2010 - 01:11 AM

I don't really know what you mean about "Double not working in print". Here is a little test program that simulates a session attribute.
import java.util.HashMap;
import java.util.Map;

public class BogusDouble {
  public static void main(String[] args) {
    Map<String, Object> session = new HashMap<String, Object>();
    session.put("Key", 0.5); // this double gets auto-boxed to a Double
    Object o = session.get("Key");
    System.out.println("Object:  " + o);
    Double dD = (Double) session.get("Key");
    System.out.println("Double:  " + dD);
    double dd = (Double) session.get("Key"); // this Double gets auto-unboxed to the declared double
    System.out.println("Double:  " + dd);
  }
}



The map simulates the session storage exactly, as far as you're concerned (they just have different method names as the session object wraps a simple Map). This shows you all three ways of "getting" the attribute, as an Object, a Double, and a double (as of Java 1.5, before that the third way is a bit different, as is the declaration, and population, of the map). All three variations print the same value, of course.

Also, if you have something saved in the session, then there is no forwarding. Simply retrieve it from the session, of course.

I think you need to simply go through the JEE 5 Tutorials or the JEE 6 Tutorials or maybe even the J2EE 1.4 Tutorials, depending on which Enterprise Server/Web Container you're using, and its version.

This post has been edited by masijade: 09 September 2010 - 01:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1