Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generating numbers from range (using regex)

 
D Preethi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10427
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic