Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing a range after finding a dash (console input)  RSS feed

 
Steve Ross
Greenhorn
Posts: 1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

This seems really simple but I am not able to think of a viable solution. I'm trying to have a user enter numbers and throw them in an array. Next, if they give me something like 2-4 I want to print "2, 3, 4." For this, should I make the array an arraylist to begin with so I can add where needed? Is there a simple way to accomplish this?

 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not clear exactly what you're trying to do. If you get a range, like 2-4, do you want to just print the numbers, or do you need to put them in array? Will you be getting a mix of lists of number and ranges, like 1,2,3, 10-15, or is it just one or the other. To parse the range, you're going to want to parse out the substring (if necessary), then break it into a lower bound and upper bound. Convert both of those to numbers, and loop through the numbers, printing them as you go, or adding them to an ArrayList, or even a regular array? (If you just have a single range, you'd be able to compute the size of an array before looping through the numbers. If you have the list of numbers, it would be easier to use an ArrayList.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!