Results 1 to 6 of 6
  1. #1
    loftus727 is offline Member
    Join Date
    Feb 2013
    Posts
    24
    Rep Power
    0

    Default Writing a program determing if a date entered is valid or not, I need help

    Here is the code i have so far and errors that go along with it..

    This are the errors I can not seem to figure out.


    Dates.java:89: error: variable monthValid might not have been initialized
    if (monthValid==true && dayValid==true && yearValid==true)
    ^
    Dates.java:89: error: variable dayValid might not have been initialized
    if (monthValid==true && dayValid==true && yearValid==true)
    ^
    Dates.java:89: error: variable yearValid might not have been initialized
    if (monthValid==true && dayValid==true && yearValid==true)
    ^
    3 errors

    Here is the code that I have:

    Java Code:
    // ************************************************************
    // Dates.java			
    //
    // Determine whether a 2nd-millenium date entered by the user
    // is valid
    // ************************************************************
    
    import java.util.Scanner;
    
    public class Dates
    
    {
       public static void main(String[] args)
       {
          int month, day, year; //date read in from user
          int daysInMonth = 0; //number of days in month read in
          boolean monthValid, yearValid, dayValid; //true if input from user is valid
          boolean leapYear; //true if user's year is a leap year
          Scanner scan = new Scanner(System.in);
       	//Get integer month, day, and year from user
       
          System.out.println("Enter the integer for the month");
          month = scan.nextInt();
          System.out.println("Enter the intger for the day");
          day = scan.nextInt();
          System.out.println("Enter the integer for the year");
          year = scan.nextInt();
       
      		//Check to see if month is valid
          if (month < 1 || month > 12)
          {
             monthValid = true;
          }
       
       	//Check to see if year is valid
          if (year < 1000 || year >1999)
          {
             yearValid = true;
          }		
       
       	//Determine whether it's a leap year
          if ((year%4 == 0 && year%100 !=0)|| year%400 == 0)
          {
             leapYear = true;
             System.out.println("it's a leap year");
          }
          else
          {
             leapYear = false;
             System.out.println("not a leap year");
          }
       
       	//Determine number of days in month
          switch(month)
          {
             case 1:
             case 3:
             case 5:
             case 7:
             case 8:
             case 10:
             case 12:
                daysInMonth = 31;
                break;
             case 4:
             case 6:
             case 9:
             case 11:
                daysInMonth = 30;
                break;
             case 2:
                if (leapYear == false)
                {
                   daysInMonth = 28;
                }
                else
                {
                   daysInMonth = 29;
                }
                break;
          }
       
       	//User number of days in month to check to see if day is valid
          if (day <= daysInMonth)
          {
             dayValid = true;
          }
       	//Determine whether date is valid and print appropriate message
    		if (monthValid==true && dayValid==true && yearValid==true)
    		{
    			System.out.println("Date is valid");
    		}
    		else
    		{
    			System.out.println("Date is not valid");
    		}
       
       
       }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Writing a program determing if a date entered is valid or not, I need help

    If you test whether or not a month is valid you set monthValid to true but you don't set it to false if the month isn't valid; oh, and b.t.w. the valid month test is incorrect itself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,804
    Rep Power
    5

    Default Re: Writing a program determing if a date entered is valid or not, I need help

    Since there is no guarantee that your code will set the monthValid and similar fields to any value, you need to initialize them to something. Otherwise the compiler complains that the variables have not been initialized.

    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    loftus727 is offline Member
    Join Date
    Feb 2013
    Posts
    24
    Rep Power
    0

    Default Re: Writing a program determing if a date entered is valid or not, I need help

    Can you both be more specific as to what you mean?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Writing a program determing if a date entered is valid or not, I need help

    Quote Originally Posted by loftus727 View Post
    Can you both be more specific as to what you mean?
    Erm. no; I'm sorry.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Location
    Islamabad, Pakistan
    Posts
    25
    Rep Power
    0

    Default Re: Writing a program determing if a date entered is valid or not, I need help

    Hope you understand this . In java there are three types of variables
    -->Instance variable (Associated with object)
    --> Static variables (Variables with scope static also called class variables)
    --> Local variables ( Which are inside the parenthesis of any local method . method can be main)

    In java you are allowed to declare Insatnce variables and you may intialize it like:
    Java Code:
    class Test{
    int roll_no;
    String name="Jamil|;
    //Both of these are instance variables . its your will you want to intialize it or not.
    }
    But java dont allow to use local variables without intialization like
    Java Code:
    class NewTest{
    public static void main(String args[]){
    int a;
    System.out.print(a);// It will be compile time error coz local variables cant be used without intialization.
    }
    }
    So the problem in your code is that all these three variables are local and you are using them without initalization.
    Solution is to initalize them.


    By the way your conditions are not right i think.
    Java Code:
    if (month < 1 || month > 12)// Should be month>=1 || month<=12
          {
             monthValid = true;
          }
        
        //Check to see if year is valid
          if (year < 1000 || year >1999) // This should be year>=1000 || year <=1999
          {
             yearValid = true;
          }
    Regards
    Jamil
    Last edited by Jamil37037; 03-05-2013 at 09:37 AM. Reason: Adding things

Similar Threads

  1. help in writing a program?
    By baldy100 in forum New To Java
    Replies: 3
    Last Post: 01-10-2013, 05:06 PM
  2. stuck on writing Date application
    By gabrielpr12 in forum New To Java
    Replies: 2
    Last Post: 01-07-2012, 09:31 PM
  3. Problem with determing mouse position!!!
    By G.rocks in forum New To Java
    Replies: 6
    Last Post: 07-15-2010, 06:09 PM
  4. Self determing the server
    By etherkye in forum Networking
    Replies: 4
    Last Post: 06-26-2009, 12:36 AM
  5. Help determing NamingEnumeration type/cast
    By BlankMan in forum Eclipse
    Replies: 1
    Last Post: 09-05-2008, 08:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •