-
August 4th, 2009, 02:57 PM
#1
Parsing a double from a string
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?
-
August 4th, 2009, 03:50 PM
#2
Re: Parsing a double from a string
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).
-
August 4th, 2009, 04:36 PM
#3
Re: Parsing a double from a string
PHP Code:
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.
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
-
August 4th, 2009, 04:48 PM
#4
Re: Parsing a double from a string
You can also use:
Code:
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.
-
August 5th, 2009, 08:11 AM
#5
Re: Parsing a double from a string
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!
-
August 5th, 2009, 11:04 AM
#6
Re: Parsing a double from a string
You could also do the following:
Code:
Pattern p = Pattern.compile("-?(\\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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|