Else without if problem

Hi All,
just wodering if someone could help a noob with and else without if problem. The compiler seems to have a problem with the last else of the boolea. Thanks again in advance:
static boolean Enterdate (boolean valid)
          System.out.println("Please Enter Student Date of Birth in ddmmyyyy format");
          String date = DosInput.getString();
          if (date.length() == 8)
               String extractday = date.substring(0,2);
               int day = Integer.parseInt(extractday);
               String extractmonth = date.substring(2,4);
               int month = Integer.parseInt(extractmonth);
               System.out.println(month);
               String extractyear = date.substring(4,8);
               int theYear = Integer.parseInt(extractyear);
               System.out.println(theYear);
               if (theYear >= 2004)
               valid = false;
               int maxMonthDays = 0;
               switch (month)
               {//start of switch
               case 1:
               case 3:
               case 5:
               case 7:
               case 8:
               case 10:
               case 12: //this will be executed when all the above are true - no breaks !!
                    maxMonthDays = 31;
                    break;
               case 4:
               case 6:
               case 9:
               case 11: //this will be executed when the above 3 are true -no breaks!!
                    maxMonthDays = 30;
                    break;
               case 2://the month is february
                    maxMonthDays = 28;
                    if(theYear %400 ==0)
                    {System.out.println("leap year - div 400");
                         maxMonthDays = 29;
                    else//year divisible by 4 and not divisible by 100
                         if((theYear %4 == 0) && !(theYear %100 == 0))
                         //all these brackets are needed !!
                    {System.out.println("leap year - div 4");
                           maxMonthDays = 29;
                    else
                         System.out.println("not a leap year ");
                         break;
               }//end of switch block
               else
                    valid = false;
return (valid);
     } //end of method

What you should be doing is returning false immediately if the input string is not eight characters in length and all digits. (Both are prerequisites for the proper functioning of your app.)
A couple more suggestions won't hurt:
(1) A method should do one thing and do it well. Your method is doing two things: taking input from a user and checking to see if a string represents a valid date. You can never use this method in situations where you'd like to validate a string as a date and not take input from a user. It cuts down on the usefulness of your method. Get that input stuff out of there and have the method just validate a string as a date.
(2) The name of the method should immediately suggest what it does. Ditch that Enterdate name and call it isValidDate. Make sure you follow the Java coding standards and start the name with a lower case letter.
(3) That switch statement sure is big and ugly. Why not just have an array of integer values, one per month?
(4) I'd suggest another method, isLeapYear, that will take an int and return true if the number represents a leap year, false if not. You might find it handy to call elsewhere.
It's called decomposition. Learn how to do it.
This must be a school assignment. You're required to do this a certain way.
I think I can write this method in many fewer lines. I'd do it like this:
public boolean isValidDate(final String dateStr)
    try
        java.text.DateFormat formatter = new java.text.SimpleDateFormat("ddmmyyyy");
        java.util.Date dateValue = formatter.parse(dateStr);
        return true;
    catch (java.text.ParseException e)
        return false;
}MOD

Similar Messages

  • I can't complete the 5.0 update without choosing icloud, anyone else having this problem?

    I can't complete the 5.0 update without choosing icloud, anyone else having this problem?

    You can't just select "skip"?  It's at the bottom and is in smaller print.

  • HT1947 Does anyone else have my problem with atv3? Plays films in iTunes without any problems but on music every time I use the remote app on my iPhone 3GS it stops playing after around 5 tracks in. Anyone having this problem?

    Does anyone share my problem with atv3 I can play films through iTunes without any problems. When I start playing music using the remote app on my iPhone 3GS after around 5 tracks in it just stops playing and seems to lose my connection. Hoping this is human error on my part?

    I think this is probably a sign that your network could be better than it is.
    Intermittent problems are often a result of interference. Interference can be caused by other networks in the neighbourhood or from household electrical items.
    You can download and install iStumbler (NetStumbler for windows users) to help you see which channels are used by neighbouring networks so that you can avoid them, but iStumbler will not see household items.
    Refer to your router manual for instructions on changing your wifi channel or adjusting your multicast rate.
    There are other types of problems that can affect networks, but this is by far the most common, hence worth mentioning first.

  • My new Iphone 5c drops battery life by 20% in an hour without me touching it? I believe it is a bad battery has anyone else had this problem?

    I haven't touched my Iphone 5c for an hour besides a few texts and it as dropped its battery life by 20% I also took it off the charger (at three in the morning) at 90% and woke up to a 70% battery life. I got this phone yesterday and let it die and recharge like you are supposed too, I think it is a bad battery but I am wondering as anyone else had this problem before? I want to know if I should be taking this back or not? Also while charging last night the phone got very hot which is also very concerning.

    If you aren't receiving password reset emails and your security questions don't work, someone has hijacked your account.
    This happened to me.
    You need to use Expresslane to contact iTunes support to regain access to your account ASAP.

  • HT1657 Hi, at the moment I can only seem to watch a rented movie once - previously I used to be able to watch it as many times as I liked during the 48hr window.  Anyone else have this problem?

    Hi, at the moment I can only seem to watch a rented movie once - previously I used to be able to watch it as many times as I liked during the 48hr window.  Anyone else have this problem?

    This doesn't seem right.
    I paid for a rental and downloaded an HD movie, Avatar. I could start watching within about a minute, it played without skipping a beat.
    My wife started watching the movie the next night and got about half way through.
    The next day, she tried watching the rest, and selected the film and clicked on "resume watching".
    But instead of streaming and playing like it should, we got the spinning wheel of death followed by a message "Ready to play in 1 minute" which soon became "Ready to play in 2351 minutes"!
    Additional information that I can add is that it was Sunday afternoon which is peak time for internet usage and I'd just exhausted my data allowance before the speed limiting kicks in. Basic internet using browsers at that time was really slow.
    So it was as though the movie we were going to resume watching was going to be downloaded from the iTunes Store again, and since my connection had been speed limited, it was going to take forever. I thought you downloaded a rental to your hard drive and it deleted itself after it expired? So any subsequent streaming to AppleTV would be coming direct from your hard drive. It shouldn't have been affected by the speed of my internet connection at that time, surely. Anyone else experienced this?
    Actually, I just noticed this post, I'm sure it's related...:http://discussions.apple.com/thread.jspa?messageID=12398832&#12398832

  • Ive updated my iphone 4 to ios5 and the music will not play atall even though its all on the phone is anyone else having this problem?

    Obviously Ive had the same errors originally but the software has updated fine now. But for some reason the music on the phone will not play atall! with or with out headphones. It seems too just be staying at 0.00mins and even if I fast forward it it just stays at the particular mins fast forwarded to. Has anyone else had this problem?

    Similar symptom here.  I gathered from other threads that by removing all the songs and re-sync them back to iPhone4 would sort the problem!  I tried with one album and it seemed to have work fine!  So I removed all the songs and transferred them back again!  However, I could not launch Music app any more!  Click on Music and it just stopped, i.e. could not be launched!!!
    However, I noticed that I could still play all the songs through headphone control.  But if you play without the headphone plugged in, no sound comes out of the phone speaker!!!!!
    Aarrrgggggghhhhhhhhhhhhhh!!!!!!!!
    Apple - surely you must seriously look into this and come out with resolutions by now!  Would SJ step in by now????? RIP SJ!  Apple, in fact all of us, miss you - with diferent reasons !!!

  • Any one else got email problems since iOs6.1 update? Can receive but not send.

    Since updating to iOs6.1 I can receive email but not send using the iPads generic mail app. Has anyone else experienced this problem? Any suggestions about how to resolve this. Have deleted accounts and added again without success.

    iOS: Unable to send or receive email
    http://support.apple.com/kb/TS3899
    Can’t Send Emails on iPad – Troubleshooting Steps
    http://ipadhelp.com/ipad-help/ipad-cant-send-emails-troubleshooting-steps/
    iPad Mail
    http://www.apple.com/support/ipad/mail/
    Try this: Delete the account in Mail and then set it up again.
     Cheers, Tom

  • I recently updated my iTunes to the new version. For some reason the titles and details of songs on the SONGS page do not appear, although the songs will play. Has anyone else had this problem? Every other page seems to be ok.

    I recently updated my iTunes to the new version. When I opened it I found that there were no details listed on the SONGS page. On all other pages the relevant pictures/details appear. Although there are no details showing on the songs page, the songs are obviously there as when you click on a line it plays the song that should be listed. Has anyone else had this problem? (I am using a Windows XP Pro system on my Laptop).

    Did anyone ever suggest you reset your PRAM?
    http://docs.info.apple.com/article.html?artnum=2238
    Also might want to try resetting the SMC, although I
    doubt there's an issue there.
    http://docs.info.apple.com/article.html?artnum=304123
    I'm getting tired of people who supposedly know what
    they're doing simply guessing that resinstalling the
    OS might solve the issue. It's like trying to dissect
    a frog with a sledge hammer, and wondering why you
    didn't learn anything about anatomy in the process.
    These sorts of things, although bizarre, should be
    (and probably are) fixable without major surgery.
    Thanks for the suggestion... but I think my first question would be why these parameters would need re-setting? This is a new machine... It has never been shut down hard. It is used at most 10 hours per week... mostly using Office 2004 for MAC. I need to figure this out as I will soon need it as a Cs2/CS3 - Aperture workhorse.
    I too agree that it is best not to use a sledge when a scalpel is needed... but as a novice to the MAC world I may have been misled by Apple support. I seems to have fixed the symptoms ( some) but appears NOT to have addressed the cause... which is what I am after.
    Would TechTool Pro detect a bad HD or RAM?
    Thanks....

  • Whenever I ask Siri to send a message to a contact, it will attempt to send it as an iMessage. This is a problem because it will attempt to send iMessages to contacts of mine which aren't even iPhone numbers. Is anyone else having this problem?

    Whenever I ask Siri to send a message to a contact, it will attempt to send it as an iMessage. This is a problem because it will attempt to send iMessages to contacts of mine which aren't even iPhone numbers. Is anyone else having this problem?
    So, this is a problem because when Siri attempts to send iMessages to those without iPhones it, of course, fails.
    For example, if I say to Siri, "Tell my mother 'Hello.'" It will try to send an iMessage. My mother has an Android phone, so the iMessage just gets lost in cyberspace.
    I use Siri quite often, this problem only occured about two hours ago.
    Can anyone help me with this?
                   Thanks.
                        Thomas

    I had major issues with the iPhone 4s battery, however it’s resolved.
    The tech who set the phone up at the Apple store did so with little training.
    if you have a mobile me account. First go and move all your data to the cloud by going on your computer and logging in at me.com/move. The cloud has replaced mobile me, so there is no need for those two accounts
    Also make sure that for any of your email accounts you set them up to fetch, not push. My tech person set them all to have the email servers push data to the phone. The new iphone4s antenna is extremely strong so it will continually try to access stuff that is pushed–***** a lot of battery life doing this. It makes it worse if you have exchange 2010 accounts. Something about changes made to exchange really suck battery life from devices that access such accounts.
    turning of locator and the push notifications from facebook--they have a lot!

  • HT1600 Since the latest up-date I can't see any movies.  I always returns to the main menu.  Is anyone else having this problem and how do I fix it?

    Since the latest up-date I can't see any movies.  It always returns to the main menu.  Is anyone else having this problem and how do I fix it?  We have gone 3 days without the use of renting movies.

    Welcome to the Apple Community.
    First you should try logging out of your iTunes Store account (settings > iTunes Store > accounts) and then back in again.
    If that doesn't help, try resetting the Apple TV (Settings > General > Reset > Reset all settings). You should also try restarting your router.
    If both of the above don't help, you should try a restore (Settings > General > Reset > Restore).

  • Ever since I downloaded the ICloud I am unable to use my camera, it will turn on but the screen will not show the camera view, anyone else have this problem or what I can do to fix?

    Ever since I downloaded/installed the ICloud IOS5 I can not use the camera icon on my Ipod. I can click on the icon, the screen will lighten just a little but it is not letting me see what is in the camera view. Any one else have this problem or know how I can fix it?

    Make an appointment at the genius bar are your nearest Apple store. At this point there is nothing you can do without a computer, so if you don't have one and don't know anyone who has one that you can use the Apple store is a good fallback.

  • What do I do if I can't upload photos into my photobook?   My upload always starts but then stops.  All I have the option of doing then is cancel or retry.  Has anyone else had this problem and found a solution?

    What do I do if I can't upload photos into my photobook?  Every time that I try, the upload starts but then stops.  Then the only options I have are to cancel or retry.  Has anyone else had this problem and how have they solved it?

    I solved the problem. Had nothing to do with Mac or iphoto. Problem must be my the internet connection to my provider.
    Upload at my office worked without any problem. One problem solved, now i am aware with a lot of discussions with my providers helpdesk  ;-)

  • When I try to open a pages document on my mac book pro it says "The file 'thesis.pages' couldn't be opened." But i can open the same file on my iPad and iPhone without a problem.

    When I try to open a pages document on my mac book pro it says "The file 'thesis.pages' couldn't be opened." But i can open the same file on my iPad and iPhone without a problem.

    Thanks very much for the instructions.  I encountered this problem this morning myself and the recommendation fixed the problem immediately.  When things like this happen with iCloud and Pages it makes me wonder if I might be better off using something else, having just recently had to fix the problem that the current Pages can't read documents I created with a much earlier version until they were "upgraded" by an interim version.

  • I just upgraded my apple TV and now when I rent a movie on iTunes, instead of it playing instantly, it takes 60 hours to load. Anyone else have this problem?

    i just upgraded my apple TV and now when I rent a movie on iTunes, instead of it playing instantly, it takes 60 hours to load. Anyone else have this problem?

    Apple Support, I have just purchased 3 movies 2 from my account and 1 from my wifes account and both systems are stating that it will take 2 days to download.  I have a FIOS 150 megabit per second up and 150 megabit per second down busiuness class fiber service to my location.  I have tested downloading 10Gbyte files from two other locations and both work fine.  I additionally went out and have tested doing speed tests (http://www.speedtest.net/) from over 10 other locations all over the US and all are over 75 magabits per second download speeds with a few hitting 100 and 125 megabit per second speeds.  What is wrong with your servers or your internet connection????
    This is VERY FUSTRATING!!!
    I have additionally tried to download them with my Apple TV and two different PCs all wired on Gigabit Ethernet with jumbo frames enabled.  I have tried differnet DNS servers also.  I can upload and download files to my office 50G in less than 1 hour.  I have also download movies from 2 other movie sites without any problems.

  • Unable to open my Nemo's reef for last 4days. Has anyone else had this problem

    Unable to open my Nemo's reef for last 4days. Has anyone else had this problem. It seemed to occur after the last content download

    I too have been unable to open Nemo's Reef properly since January 31 2014. I have used it without problems since August 2013. I recall it self updated around midday and then ever since it's failed to get beyond the opening page of what's in the reef. If I tap the back button it goes back to the iPad screen of apps. If I tap to buy a suggested plant it also disappears from the screen. However I have managed to collect my pearl gifts/sand dollars daily on that initial screen.
    I just cannot buy a plant or get out of this screen without the crash. Just going nowhere.
    Rather frustrating at level 37 and about £80 overall spent in pearls to build the reef and get beautiful fish whilst recovering from surgery past six months. I have found the game very calming. I've really enjoyed this game which past level 8 is surely intended for adults and more strategic play.
    This morning I switched on and there was instead a full screen advert. I hit the x button and it just crashed again. Now it shows my own reef on loading, but instantly crashes even faster.
    Is this an iPad problem or an apple problem. From an Apple point of view FWIW I have noticed my screen gets very hot when playing games such as Bejewelled Blitz too.
    It would be good to know if others have this nemo or/ and overheating iPad problem too. What is the solution?

Maybe you are looking for