• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

casting string to a double

I want cast a String into a double
I would also like the code to turn the string 8808 (for example) into an integer
0
obirdman
Asked:
obirdman
1 Solution
 
objectsCommented:
double d = Double.parseDouble("1.6");
int n = Integer.parseInt("8808");
0
 
antonsigurCommented:
Here is a solution with error handling, you should always do that if you are parsing for example user input.. you never know if your user won't just type "abcd" where you wanted "1234"

        String a = "123.4";
        String b = "2394";
       
        double d = 0;
        int i = 0;

        try {
            d = Double.parseDouble(a);
            i = Integer.parseInt(b);
        } catch (NumberFormatException e) {
            e.printStackTrace();  
        }
0
 
objectsCommented:
> Here is a solution with error handling

Any code should have error handling.
I really don't think it is necessary to show it when giving examples.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
antonsigurCommented:
Yes, you are right, but obirdman just doesn't seem to know java very well, so I'm just showing him how that works! I think hi should give you the points, because you answered before me!
0
 
CleanupPingCommented:
obirdman:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to objects

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now