aspose file tools*
The moose likes Beginning Java and the fly likes trouble using .useDelimiter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "trouble using .useDelimiter" Watch "trouble using .useDelimiter" New topic
Author

trouble using .useDelimiter

Sebastion Hill
Ranch Hand

Joined: Jul 10, 2009
Posts: 48
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

Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
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?


SCJP6
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Jul 10, 2009
Posts: 48
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

Joined: Jul 10, 2009
Posts: 48
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

Joined: Jul 10, 2009
Posts: 48
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Jul 10, 2009
Posts: 48
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

Joined: Jul 10, 2009
Posts: 48
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 *
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trouble using .useDelimiter