| Author |
generating numbers from range (using regex)
|
D Preethi
Ranch Hand
Joined: May 24, 2008
Posts: 68
|
|
Hi All,
I want to generate a series of numbers based on the range provided by a user. Like, if he provides 30-100, I want to generate numbers - 30,31,32,33,34,35...97,98,99,100. Is this possible through regular expressions in java?
Thanks
Preethi
|
 |
D Preethi
Ranch Hand
Joined: May 24, 2008
Posts: 68
|
|
Just wanted to add here that I do not want to use a solution like:
I am looking for a more optimized way. That is, if java itself has some class to return the list of numbers given an upper bound and lower bound. Something like this:
http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/math/IntRange.html
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Java itself doesn't. You can use IntRange though, or use a simple collection like a Set (use TreeSet or LinkedHashSet to keep the range ordered) or a List implementation.
As for the regular expression, you can create one to extract the lower and upper bounds, but not to generate the numbers. You need to combine both solutions - one to extract the bounds, one to generate the numbers based on those bounds.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4734
|
|
D Preethi wrote:Just wanted to add here that I do not want to use a solution like:I am looking for a more optimized way...
It may be worth pointing out that if you already have lowerbound and upperbound and all you want to do is print out the numbers in the range, that code is about as optimized as it gets. I suspect you mean more generic or flexible.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
 |
|
|
subject: generating numbers from range (using regex)
|
|
|