If you didn't get a chance to check out part I please read it first so this post makes a little more sense. My friend called me and said that he was done with his exercise and since it wasn't due for another 2 days he was going to try for extra credit. The extra credit was to have the program flow a little bit better and had to meet the following requirements.
- When the user clicks the cancel button the program must exit
- When the user enters something that doesn't meet our validation they must be prompted to re enter that data.
First off we need to solve this cancel button riddle. It happens to be a pretty simple answer. When you take input from the user and they click cancel, null is returned. So we can check for null and just exit the program.
Now the next requirement is a little bit trickier. First off this program is impossible to write the way its currently constructed. If we ask for the day first there is no way for us to validate at that point if what they are entering is correct. Now if we ask for the month first then we can set our max days variable. So in this case to make it actually work I am going to ask for the month, day and then year. Now to accomplish this we have to use a while loop. What we to do is keep asking for the users input while a condition is true. In the case of our month we can do the following. What we are saying is while m is 0 (default setting) and greater than 12 (invalid month) keep asking for the input.
Pretty simple but if you haven't worked with swing in awhile (this guy) it can be tricky. Here is the complete example.
Update:
I just realized that our leap year requirement would not work the way I wrote it. The only way for this example to work correctly is to ask for the info in the exact opposite way the exercise wanted it. You need to know the year so that when you asking for the month you can set the correct number of max days and you need to know what the max days are when you are asking for the day.
