homework help? doubles.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itgetsharder
    New Member
    • Oct 2007
    • 9

    homework help? doubles.

    Hey, i was wondering if anyone could help me.
    i have two questions that i cannot complete for a homework assignment:

    [1] This method should convert its parameter (a string like "3.1415") to the corresponding value of type double. If the string supplied is not a valid number, it should return 0.0 as its result. Note that you can use the method Double.parseDou ble() to do the hard work for you.
    --------------------------------------------------------------------------------

    [2] Modify your convertDouble method to ignore any extra characters at the end of the string, so that convertDouble(" 3.1415xxx") will produce the value 3.1415 as its result.


    Here is the coding that i have also done so far:

    [1]
    Code:
    public double convertDouble (String value) 
        {
    while(true) 
    {
      try 
    {
        int number = Integer.parseInt(value);
        break;
      }
     catch(NumberFormatException e) 
    {
        value = "0";
        }
    }
    return Double.parseDouble(value);
    }
    With this question i get an error when i try to type "-1.12E8-15" , i should get "-1.12E8-15" returned. Instead i get 0.

    [2]
    Code:
     
      public double convertDouble (String value) 
        {
                for(int i = 0; i < value.length();i++)
                {
                            
            value = value.replaceAll("\\D*$","");
            }
        return Double.parseDouble(value);
        }
    With the above question, when an empty string is entered ("") i get an error. Also when "null" is entered i get an error. both of these should return 0.

    I have been working on this for a while now and any help would be appreciated.

    thanks in advance.
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by itgetsharder
    Hey, i was wondering if anyone could help me.
    i have two questions that i cannot complete for a homework assignment:

    [1] This method should convert its parameter (a string like "3.1415") to the corresponding value of type double. If the string supplied is not a valid number, it should return 0.0 as its result. Note that you can use the method Double.parseDou ble() to do the hard work for you.
    --------------------------------------------------------------------------------

    [2] Modify your convertDouble method to ignore any extra characters at the end of the string, so that convertDouble(" 3.1415xxx") will produce the value 3.1415 as its result.


    Here is the coding that i have also done so far:

    [1]
    Code:
    public double convertDouble (String value) 
        {
    while(true) 
    {
      try 
    {
        int number = Integer.parseInt(value);
        break;
      }
     catch(NumberFormatException e) 
    {
        value = "0";
        }
    }
    return Double.parseDouble(value);
    }
    With this question i get an error when i try to type "-1.12E8-15" , i should get "-1.12E8-15" returned. Instead i get 0.

    [2]
    Code:
     
      public double convertDouble (String value) 
        {
                for(int i = 0; i < value.length();i++)
                {
                            
            value = value.replaceAll("\\D*$","");
            }
        return Double.parseDouble(value);
        }
    With the above question, when an empty string is entered ("") i get an error. Also when "null" is entered i get an error. both of these should return 0.

    I have been working on this for a while now and any help would be appreciated.

    thanks in advance.
    @ convertDouble(S tring) why use break? what's the purpose?

    "-1.12E8-15"?
    maybe "-1.12E(-7)"
    is equivalent to -1.12 x 10 raise to the power of -7
    and it is also equivalent to -0.000000112, and if you format it with %.10f, then you can see the value... instead of zero....

    use try/catch when converting doubles....
    Exception will occured when convert null or "", then if it happens, try to set value = '0'

    Correct me if im wrong,
    Sukatoa (Shadow Shaman)
    Last edited by sukatoa; Feb 25 '08, 01:26 AM. Reason: change something

    Comment

    Working...