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!
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 :/
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?
Joined: Nov 28, 2011
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.
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.)
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.
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?