Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-03-2010, 03:24 AM   PM User | #1
sackstein
Regular Coder

 
Join Date: Jan 2009
Posts: 160
Thanks: 40
Thanked 1 Time in 1 Post
sackstein is an unknown quantity at this point
J2ME problem parsing string to double

I don't know if this is talked about here since it is mobile development. But I am taking a class in jave me and am having problems parsing a string to a double in the following code:

Code:
 public void convertTemperature()
    {

        double result;
        TextField num = new TextField("Enter the number of units you would like to convert", "", 50, TextField.ANY);
        ui_form.append(num);
        str = num.getString();

        try
        {
            numUnits = Double.parseDouble(str);
        }
        catch (NumberFormatException nfe)
        {
        }

        //double fromNum = Double.parseDouble(str);

        TemperatureConverter c  = new TemperatureConverter("Celsius", "Fahrenheit", "This is a temp converter",
                        "Temperature converter", "Temp");
        result = c.convertCF(numUnits);
        res = "" + result;

    }//End convertTemp()
for some reason it is always parsing it as a 0. Can you see anything wrong with this code?
sackstein is offline   Reply With Quote
Old 12-03-2010, 06:56 AM   PM User | #2
cs_student
Regular Coder

 
cs_student's Avatar
 
Join Date: Oct 2009
Location: ~/
Posts: 195
Thanks: 2
Thanked 22 Times in 22 Posts
cs_student is an unknown quantity at this point
I don't see anything wrong with it off the top of my head.

What outputs as 0?

There is the possiblity that you are getting a NumberFormatException and numUnits is already assigned to 0, and never gets changed.

What is the contents of str before you try to parse it into a double?
__________________
Get GNU/Linux - Play Ogg - Vim
Using Arch Linux x86_64, Xorg + xmonad
cs_student is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:20 AM.


Advertisement
Log in to turn off these ads.