Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Leap year program

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Leap year program

    I just started learning java language. One of my assignments for this week is writing a leap year program.
    Basically, when a user input a year, month, and day, the program has to find out if the date is valid or not.


    Please enter a year between 1200 and 2050: 2010
    Please enter a month: 5
    Please enter a day: 32
    5/32/2009 is not a valid date.

    This is what I should get.

    This is what I wrote, and I am keep getting semantic error for the requirement.
    // ****************************************************************
    // ValidDate.java
    //
    // Determine whether a date entered by the user
    // is valid.
    //         
    // ****************************************************************
     
    import java.util.Scanner;
     
    public class ValidDate {
     
        public static void main(String[] args)   {
     
        int month, day, year, february=28, check=0;   //date read in from user
        int daysInMonth=0;        //number of days in month read in 
     
     
          Scanner keyboard = new Scanner(System.in);
     
          System.out.println("Please enter a year:\t");
          year = keyboard.nextInt();
          System.out.println("Please enter a month:\t");
          month = keyboard.nextInt();
          System.out.println("Please enter a day:\t");
          day = keyboard.nextInt();
     
          if(month == 1){
              daysInMonth = 31;
            }
          if(month == 3){
              daysInMonth = 31;
            }
          if(month == 4){
              daysInMonth = 30;
            }
          if(month == 5){
              daysInMonth = 31;
            }
          if(month == 6){
              daysInMonth = 30;
            }
          if(month == 7){
              daysInMonth = 31;
            }
          if(month == 8){
              daysInMonth = 31;
            }
          if(month == 9){
              daysInMonth = 30;
            }
          if(month == 10){
              daysInMonth = 31;
            }
          if(month == 11){
              daysInMonth = 30;
            }
          if(month == 12){
              daysInMonth = 31;
            }
     
          if(year%100 != 0 && year%4 == 0){
              daysInMonth = 29;
            }
          if(year%100 == 0 && year%400 ==0){
              daysInMonth = 29;
            }
     
          if(year<1200 || year>2050){
              System.out.println("not valid");
            }
          else{
              if(daysInMonth <= day && daysInMonth < 32){
                  System.out.println("valid");
                }
              else{
                  System.out.println("not valid");
                }
            }
        }
    }
    Last edited by copeg; September 17th, 2010 at 08:49 AM. Reason: Code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Leap year program

    I am keep getting semantic error
    Can you post more info about this error?
    What happens when you execute the program?
    If the output is wrong, please explain what is wrong and what you want it to be.

    Why do you do the following:
    daysInMonth = 29;

    For what month is that true?

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Leap year program

    daysInMonth = 29
    is for February. If an user inputs a date like 2/29/2010. Then, the program compares the decide whether there is 29th day on February of 2010. Since there is no such date apprently, the program should say "invalid," but my program is keep saying valid. Also, if I enter the 5/32/2010, the program says the date is valid as well which is not right.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Leap year program

    Can you show the console for when you execute your program?
    I don't see how your code will allow the user to enter this value: 5/32/2010

    When printing out error an error message you should show the value this is incorrect in addition to saying its invalid. For example:
    System.out.println("not valid");
    should say what(yr, day, month) is invalid and show invalid value.

    daysInMonth = 29;
    Is this for ALL the months in a leap year?

  5. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Leap year program

    use switch code..

    refer to this.. a simple calendar
    public class MyCalendar
    {
        private final int newYear1901=2;//Jan. 1, 1901, Tue(2)
        private int month;
        private int year;
     
        public MyCalendar(int month, int year)
        {
            this.month=month;
            this.year=year;
        }
     
        public MyCalendar()
        {
            this(1,2010);
        }
     
        public int newYearDay(int year)
        {
            int elapseYear=year-1901;
            int countLeapYear=(elapseYear)/4;
            return(newYear1901+elapseYear+countLeapYear)%7;
        }
     
        public boolean isLeapYear(int year)
        {
            return (year%4)==0;
        }
     
        public int monthDays(int month, int year)
        {
            int days=-1;
            switch(month)
            {
                case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                days=31;break;
                case 4: case 6: case 9: case 11:
                days=30;break;
                case 2:
                days=(isLeapYear(year))?29:28;
            }
     
            return days;
        }
     
        public int firstDayMonth(int month, int year)
        {
            int newYear=newYearDay(year);
            int totalDays=0;
            for(int i=1;i<month;i++)
            {
                totalDays+=monthDays(i,year);
            }
            return(newYear+totalDays)%7;
        }
     
        public String monthName(int month)
        {
            String name="";
            switch(month)
            {
                case 1:name="January"; break;
                case 2:name="February"; break;
                case 3:name="March"; break;
                case 4:name="April"; break;
                case 5:name="May"; break;
                case 6:name="June"; break;
                case 7:name="July"; break;
                case 8:name="August"; break;
                case 9:name="September"; break;
                case 10:name="Ocotber"; break;
                case 11:name="November"; break;
                case 12:name="December"; break;
            }
            return name;
        }
     
        public void displayChart()
        {
            String[]dayNames={"SUN","MON","TUE","WED","THU","FRI","SAT"};
            System.out.printf("\n\n%s\t%d\n", monthName(month),year);
        System.out.println("----------------------------------------------------");
     
            for(String d:dayNames)
            System.out.print(d+"\t");
            System.out.println("\n----------------------------------------------------");  
     
            int i=0;
            for(;i<firstDayMonth(month,year);i++)
            System.out.print("\t");
     
            for(int j=1;(i<42 && j<=monthDays(month,year));i++,j++)
            {
                if((i%7)==0)
                System.out.printf("\n%3d\t",j);
                else
                System.out.printf("%3d\t",j);
     
            }
            System.out.println("\n----------------------------------------------------");  
     
        }
     
        public static void main(String[]args)
        {
            try
            {
                System.out.print("YEAR: ");
                int year=new java.util.Scanner(System.in).nextInt();
                System.out.print("MONTH: ");
                int month=new java.util.Scanner(System.in).nextInt();
                MyCalendar m= new MyCalendar(month,year);
     
                m.displayChart();
            }
            catch(Exception e)
            {
                System.out.println("INVALID INPUT..Enter Number");
                    System.out.print("YEAR: ");
                int year=new java.util.Scanner(System.in).nextInt();
                System.out.print("MONTH: ");
                int month=new java.util.Scanner(System.in).nextInt();
                MyCalendar m= new MyCalendar(month,year);
     
                m.displayChart();
            }
        }
    }//end of class

    i hope it helps..

Similar Threads

  1. need idea for final year project
    By asadkhan in forum Java Applets
    Replies: 0
    Last Post: August 22nd, 2010, 01:36 AM
  2. Getting date based on days past year
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: July 14th, 2010, 04:06 PM
  3. Day,Month,Year Concatenation Error
    By srinivasan_253642 in forum JDBC & Databases
    Replies: 1
    Last Post: January 22nd, 2010, 04:40 AM