# generating numbers from range (using regex)

D Preethi

Ranch Hand

Posts: 68

posted 5 years ago

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

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

Posts: 68

posted 5 years ago

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

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

posted 5 years ago

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.

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 - OCEJPAD 6

How To Ask Questions How To Answer Questions

posted 5 years ago

It may be worth pointing out that if you already have

Winston

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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |