Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check if DOB entered as a string is valid

 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4369
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would insist they include the "unnecessary" zeros. that will make your problem much simpler.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Paul Clapham
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kd Martin wrote:

To check for only one character, use charAt:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic