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);
- }
[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);
- }
I have been working on this for a while now and any help would be appreciated.
thanks in advance.