SCJP6
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?
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.,
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Consider Paul's rocket mass heater. |