wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Check if DOB entered as a string is valid Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Check if DOB entered as a string is valid" Watch "Check if DOB entered as a string is valid" New topic
Author

Check if DOB entered as a string is valid

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Hello!
I'm having trouble understanding how to create this method for a DOB string input by the user:
SetDOB(String dob) will check whether the input String is in the format of MM/DD/YYYY and also with valid date. E.g., 02/30/1999 is not a valid date since February does not have 30 days, while Feb 12, 1999 is not a valid format. You should allow user to input date without unnecessary 0 in it, e.g., 2/3/1999 is valid.

I understand how to check if it is in the correct format, but I'm getting thrown off by allowing the user to leave out the unnecessary zeros. I also don't know of an efficient way to check if the date is valid. Thank you!
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

i would insist they include the "unnecessary" zeros. that will make your problem much simpler.


SCJP
Visit my download page
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

Have to disagree... you should be making things easier for the users, not harder. Forcing them to use leading zeroes would be most unfriendly.

You can look into one of DateFormat or regular expressions to help validate the input string.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

Also see this other recent post on this topic.
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
The problem here is that we've only started discussing strings and files in class, so I'm still working on a pretty elementary level. We aren't allowed to use SimpleDateFormat, or anything that hasn't been covered yet :/
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Ah, so this is beginner homework. You implied that your code was complete except for the minor detail of allowing variable-length day and month numbers. So don't you think it would be more "efficient" to start from that, rather than throwing it all away and starting from the beginning?
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
I'm sorry, I don't understand what you mean by that. I'm just working on the Student.class right now, piece by piece, and I'm stuck on that method. I have another post where the full assignment is and a few of the problems I'm having with actually understanding the assignment, files, etc. So I'm nearly finished with the Student class but for the setDOB method. After that, its on to the rest of the assignment.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Kd Martin wrote:I understand how to check if it is in the correct format, but I'm getting thrown off by allowing the user to leave out the unnecessary zeros.


So let's start with that. Otherwise your question is pretty useless; you're only going to get vague responses like "What have you got so far?" or "What do you think would be a good idea?" (Read the FAQ articles NotACodeMill and ShowSomeEffort and DoYourOwnHomework for explanations of how this forum works.)
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
I would read up a little on regular expressions and then use java.util.regex.Pattern. However if you really are not allowed to use anything not covered yet, and regex hasn't been covered, then... uh...


Costs matter. Justice lies in processes not outcomes. Crime is caused by criminals.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I believe this is an exercise in basic String processing -- so no regex -- am I right, kd?

So let's do a bit of analysis before we start implementing anything. We can have one or two digits for the month, then we have to have a slash, then one or two digits for the day, then a slash, then four digits for the year. That's a bit more complicated than your UFID example.

There's several ways to approach this (even without the advanced stuff like regex or the real-life stuff like DateFormat). Let me suggest that you should first find the two slash characters first. If you don't find two then it's not valid, right? Those two slashes should delimit the string into three substrings, which represent month, day, and year, so get those into separate variables. Check if they are numeric for a start (you know how to do that), if they aren't then the input is not valid.

At this point you have three strings which are numeric. Now there's some more validation to do: the month has to be between 1 and 12 and the day has to be between 1 and some number which depends on the month. That's a quick description, can you take that and write some code?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Kd Martin wrote:

To check for only one character, use charAt:


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: Check if DOB entered as a string is valid
 
Similar Threads
Date validation in JAVA
Calendar Class
Student File and Checking Validity of DOB and Student ID
To check the input date is future date or not
Creating array of objects from user input and saving to a file