448,804 Members | 1,576 Online
Need help? Post your question and get tips & solutions from a community of 448,804 IT Pros & Developers. It's quick & easy.

# homework help? doubles.

 P: 9 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.parseDouble() 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] Expand|Select|Wrap|Line Numbers 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] Expand|Select|Wrap|Line Numbers     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. Feb 24 '08 #1