By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,804 Members | 1,576 Online
Bytes IT Community
+ Ask a Question
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
  1. public double convertDouble (String value) 
  2.     {
  3. while(true) 
  4. {
  5.   try 
  6. {
  7.     int number = Integer.parseInt(value);
  8.     break;
  9.   }
  10.  catch(NumberFormatException e) 
  11. {
  12.     value = "0";
  13.     }
  14. }
  15. return Double.parseDouble(value);
  16. }
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
  1.  
  2.   public double convertDouble (String value) 
  3.     {
  4.             for(int i = 0; i < value.length();i++)
  5.             {
  6.  
  7.         value = value.replaceAll("\\D*$","");
  8.         }
  9.     return Double.parseDouble(value);
  10.     }
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
Share this Question
Share on Google+
1 Reply


100+
P: 539
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
  1. public double convertDouble (String value) 
  2.     {
  3. while(true) 
  4. {
  5.   try 
  6. {
  7.     int number = Integer.parseInt(value);
  8.     break;
  9.   }
  10.  catch(NumberFormatException e) 
  11. {
  12.     value = "0";
  13.     }
  14. }
  15. return Double.parseDouble(value);
  16. }
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
  1.  
  2.   public double convertDouble (String value) 
  3.     {
  4.             for(int i = 0; i < value.length();i++)
  5.             {
  6.  
  7.         value = value.replaceAll("\\D*$","");
  8.         }
  9.     return Double.parseDouble(value);
  10.     }
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(String) 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)
Feb 25 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.