Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Posts
    160
    Thanks
    40
    Thanked 1 Time in 1 Post

    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?

  • #2
    Regular Coder cs_student's Avatar
    Join Date
    Oct 2009
    Location
    ~/
    Posts
    195
    Thanks
    2
    Thanked 22 Times in 22 Posts
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •