Thread: Number in Java
View Single Post
Old Mar 31st, 2008, 8:38 PM   #6
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 311
Rep Power: 3 Fall Back Son is on a distinguished road
Re: Number in Java

Quote:
Originally Posted by RainMan View Post
The exact problem I am having is that I can't find out if there is a way to ask the user to enter a set of integers of the form 0...some integer say 10 in the form 0...10 and have java take it to mean all the integers from 0 to 10.
There are multiple ways you can do this.

If you don't insist on using that format, you can accomplish the task easily by requesting the user to enter the first and last numbers in the set. Or you can let them enter it however they want and just treat the set as the values between the first value they entered and the last value they entered. You don't have to "deal with" infinite ranges (in most applications) because for any given value, you can just check if its between the two values the user entered -- if so, its part of the set.

If you do insist on using that format, look into the classes Matcher and Pattern. Reading about Regular Expressions will be particularly helpful to you. Regular Expressions allow you to match input based on a pattern... which is exactly what you want to do. Here's the link to a tutorial from Sun... I've read it myself, its very helpful:

http://java.sun.com/docs/books/tutor...gex/index.html
Fall Back Son is offline   Reply With Quote