|
-
April 5th, 2011, 11:55 PM
#1
Stupid parsing question
Hey,
I'm being an idiot and I can't figure out how to do parse and get an integer from a string. Could someone give me an example please?
Something like:
"The action you going to take costs 5 dollars, and will take up to 3 days to complete"
How would I store the value "3" as an integer? The "5 dollars" can vary.
Thanks!
-
April 6th, 2011, 04:51 AM
#2
Re: Stupid parsing question
To convert a string to an int use Integer.parseInt(..)
Of course you have to extract the string representation of the number from the string first. You can use a regex to do that.
-
April 6th, 2011, 01:52 PM
#3
Re: Stupid parsing question
Yeah, thats the part that I don't know how to do, the regex part
-
April 11th, 2011, 04:11 AM
#4
Re: Stupid parsing question
hmmm...
Strting text = "The action you going to take costs 5 dollars, and will take up to 3 days to complete";
String[] strin;
String delimiter = " ";
strin = text.split(delimiter)
int number = Integer.parseInt( strin[strin.length-4] );
This should work, assuming the number is always the 4th element from the rear side of the string.
Probably with some modification this would also work
http://www.java2s.com/Code/Java/Regu...xpressions.htm
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
|