Click to See Complete Forum and Search --> : Parsing a double from a string
worldChanger
August 4th, 2009, 02:57 PM
Hi,
I was trying to pass a string into a double myself, but I get thrown a Numberformat exception.
Here's what I am trying to do:
String s = Litres of water collected are 12.5
I want to parse only the numeric value "12.5" into a double. I tried Double.valueOf(s); as well as Double.parseDouble(s); But they don't seem to work. Any suggestions?
MetricMike
August 4th, 2009, 03:50 PM
NumberFormatException is thrown because you're trying to convert something into a number that isn't a number. There's no number representation for "Litres" and Java isn't quite smart enough to figure that out and just ignore it.
What I'd recommend doing is taking a substring of "s" and converting just that. If you know that s will always be "Litres of water collected are X", where X is some number, go ahead and:
String sNumber = s.substring(30);
Double d = Double.valueOf(sNumber);
Alternatively, especially if you aren't sure of the length of your string you could store them separately from the get-go and concatenate them into one string for display later on. I think that's less work in general, but I could be wrong (and without seeing your program there's no real way of knowing).
Xeel
August 4th, 2009, 04:36 PM
String s = "Litres of water collected are 12.5";
double d = Double.parseDouble(s.substring(s.lastIndexOf(' ')));
As MetricMike mentioned, normally numbers are used separately from text if there is any operation with these in the program. Still if mixed text is the only input you get you should be extra carefull while extracting numbers from text. Try to find a common pattern if there's any...
For ex. I used s.lastIndexOf(' ') considering that the last part of the string is a pure number separated from the rest of the text with a space and that there are no spaces after the number. As you understand shall we eliminate the last space, or put another one after the number, or add a letter to the last text chunk - we'd have an error.
jcaccia
August 4th, 2009, 04:48 PM
You can also use:
Double.parseDouble(s.replaceAll("[^0-9.]", ""))
This will work if the number is in the middle of the string and there are further spaces after it. The only condition is that the number you want to extract is the only portion of the string with digits and a decimal point.
worldChanger
August 5th, 2009, 08:11 AM
Wow, this was really impressive. You guys are way too smart. I would have never thought of it this way. Thanks for all the replies!
keang
August 5th, 2009, 11:04 AM
You could also do the following:
Pattern p = Pattern.compile("-?(\\d+\\.?\\d+|\\d (file://\\d+\\.?\\d+|\\d)+)");
Matcher m = p.matcher(myString);
while ( m.find() )
System.out.println("Number = "+m.group());
This will find all the numbers (positive and negative, integer and floating point) in the string. For floating point numbers the decimal point must have at least one digit before and after it.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.