aspose file tools*
The moose likes Java in General and the fly likes generating numbers from range (using regex) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "generating numbers from range (using regex)" Watch "generating numbers from range (using regex)" New topic
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: 19722
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8012
    
  22

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?
Articles by Winston can be found here
 
 
subject: generating numbers from range (using regex)