• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

trouble using .useDelimiter

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I am trying to use scanner to accept user input. I would like for the user to enter in 3 sets of numbers in the date format mm/dd/yr. I thought that by using "reader.useDelimiter("/");" that I could get each integer seperated by the "/" sign.

Could someone help me out, first with my logic and then with general syntax? :-) My thinking is obviously wrong but I don't know if I use .nextInt to get the first integer value AND THEN use the delimiter for the remaining two or if it should just work regardless of when I insert the .useDelimiter

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem you're having is with Scanner(System.in).

Imagine the user types in "12/07/09". You then ask the Scanner to chop that up using "/". You think you should get "12", "07", "09" but you don't. The Scanner knows it got an input stream, so it can't be sure if "9" is the end or if something more is going to come; so it sits and waits. If you type in "/", the Scanner will then see another token, but it will see "09\n/" and you'll get an exception.

There's a few ways to solve this, and how really depends on the requirements (I assume this is for a project or something). I'll try and give you a few clues. What is the total user input for the date? "12/07/09" and what else?
Can you use that in your delimiter? Is there a way to make the Scanner "see" that at the end and not wait for more?
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the delimiter is a regex. I think that you can split with "/" but you also need to split on white space so that it can swallow the end of line character. Perhaps something like


A better solution is to use a SimpleDateFormat formatter, and appropriate format string and parse your input String to date.

e.g.,
 
Sebastion Hill
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Irwin wrote:The problem you're having is with Scanner(System.in).

Imagine the user types in "12/07/09". You then ask the Scanner to chop that up using "/". You think you should get "12", "07", "09" but you don't. The Scanner knows it got an input stream, so it can't be sure if "9" is the end or if something more is going to come; so it sits and waits. If you type in "/", the Scanner will then see another token, but it will see "09\n/" and you'll get an exception.

There's a few ways to solve this, and how really depends on the requirements (I assume this is for a project or something). I'll try and give you a few clues. What is the total user input for the date? "12/07/09" and what else?
Can you use that in your delimiter? Is there a way to make the Scanner "see" that at the end and not wait for more?



Hey Jason and Pete, thanks so much for your feedback. I am trying to digest it all right now but I did want to write back before getting back to the actual code.

Jason - I THINK I know what you're getting at ... if I know that the date is going to be a specific format then I should set up the scanner so that it doesn't just look for "/" but rather looks for the whole "##/##/##\n"???

Also, I am trying to write an Android program for cell phones, a time management program (aka a planner), and I need to learn Java first. So I am taking it in baby steps and this is where I get a due date for when an activity needs to be completed :-) Thanks so much though, on to read pete's comments now
 
Sebastion Hill
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pete stein wrote:the delimiter is a regex. I think that you can split with "/" but you also need to split on white space so that it can swallow the end of line character. Perhaps something like


A better solution is to use a SimpleDateFormat formatter, and appropriate format string and parse your input String to date.

e.g.,



Oh wow, I REALLY like the SimpleDateFormat Pete! Thanks a million. I added the Hour and Minutes as well since my next question would have been how do you add time. Now I need to figure out how to take the Date and use it in the Calendar format ... though I am wondering if I even need the calendar format now.

I have one more question in regards to scanner. I am inserting my code below (it looks gross, I am sorry) and I was wondering why I had to implement a new scanner called dateReader (Line 48). I originally tried using reader from earlier but it would always skip my input ... I figure it has something to do with my try catch statements (lines 20 & 34) but I don't know why. Could someone explain ... again, please forgive the sloppy coding, still new :-)

 
Sebastion Hill
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
again sorry if some of the println statements don't make sense ... i use it as debugging almost, to see if I make it to that line or not when running my code ... so I might just be talking to myself in some of the code :-/
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess for why you are needing to use a second Scanner is that the first one is stuck at an end of line, that you need to call

after reading in all your ints before it will respond to more input. I could be wrong, but I've seen this before.

i.e.,
 
Sebastion Hill
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Pete, your suggestion worked perfectly! :-) ... I have to put in all my try-statements now but for the most part I think I've completed my very first java program :-D ... thanks to you and the other forum members, thanks again!
 
Sebastion Hill
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
btw, does anyone know the SPECIFIC import java.?.? statement for Collections.sort(list, comparator)??? I know I can use import java.util.* but I was curious as to what I would type in if it weren't for the *
 
Marshal
Posts: 80632
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Different questionYou can be very specific and trybut read a tutorial about static import before you try that. A few links: 1 2 3 4. Note that sort method is specifically mentioned in link no 2.
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A static import of java.util.Collections.sort is a bad idea, because there is also java.util.Arrays.sort. If a static method / field is used in more than one often used class I would never even consider static imports.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic