Results 1 to 20 of 20
  1. #1
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default question on how to check values before storing

    Ok, I have a question on how to setup a check on values before they are stored.

    I have the class Date setup. What I need to do is check the values entered by the user for month/day/year to see if they are valid before storing the information. Also, put in a method to check if the year is a leap year.

    I'm pretty sure I'm doing the month, day , and leap year correctly. However, I'm not sure how to check the year for validity. I've read up on try/catch blocks and not really understanding them, and not sure if I even need to use them here.

    Here is my code:

    Java Code:
    public class Date
    {
       private int dMonth;		//variable to store the month
       private int dDay;		//variable to store the day
       private int dYear;		//variable to store the year
    
    
     		//Default constructor
    		//Data members dMonth, dDay, and dYear are set to
    		//the default values
    		//Postcondition: dMonth = 1; dDay = 1; dYear = 1900;
       	public Date()
       	{
    	  	dMonth = 1;
    	  	dDay = 1;
    	  	dYear = 1900;
       	}
    
    		//Constructor to set the date
    		//Data members dMonth, dDay, and dYear are set
          	//according to the parameters
    		//Postcondition: dMonth = month; dDay = day;
          	//               dYear = year;
       	public Date(int month, int day, int year)
       	{
    	  	dMonth = month;
    	  	dDay = day;
    	  	dYear = year;
       	}
    
    		//Method to set the date
    		//Data members dMonth, dDay, and dYear are set
    		//according to the parameters
    		//Postcondition: dMonth = month; dDay = day;
    		// 			 dYear = year;
       	public void setDate(int month, int day, int year)
       	{
    		dMonth = month;
    	  	dDay = day;
    		dYear = year;
    			
    		if(1 <= day && day <= 31)
    			dDay = day;
    		else
    			dDay = 0;
    			
    		if(1 <= month && month <= 12)
    			dMonth = month;
    		else
    			dMonth = 0;
    			
    		if(0001 <= year && year <= 2009)
    			dYear = year;
    		else
    			dYear = 0;
    				
    	}
    
     		//Method to return the month
     		//Postcondition: The value of dMonth is returned
       	public int getMonth()
       	{
     	  	return dMonth;
       	}
    
    		//Method to return the day
    		//Postcondition: The value of dDay is returned
       	public int getDay()
       	{
     	  	return dDay;
       	}
    
    		//Method to return the year
    		//Postcondition: The value of dYear is returned
       	public int getYear()
       	{
     	  	return dYear;
       	}
    
    		//Method to return the date in the form mm-dd-yyyy
       	public String toString()
       	{
    	 	return (dMonth + "-" + dDay + "-" + dYear);
       	}
    		
    		//Method to check if the year is a leap year
    		public void isLeapYear(int dYear)
    		{
    		if (dYear < 100) {
    	   	    
    			if (dYear > 40) {
    		dYear = dYear + 1900;
    	    }
    	    else {
    		dYear = dYear + 2000;
    	    }
    	}
    
    	// Is theYear Divisible by 4?
    	if (dYear % 4 == 0) {
    
    	    // Is theYear Divisible by 4 but not 100?
    	    if (dYear % 100 != 0) {
    		System.out.println(dYear + " is a leap year.");
    	    }
    	    // Is theYear Divisible by 4 and 100 and 400?
    	    else if (dYear % 400 == 0) {
    		System.out.println(dYear + " is a leap year.");
    	    }
    	    // It is Divisible by 4 and 100 but not 400!
    	    else {
    		System.out.println(dYear + " is not a leap year.");
    	    }
    	}
    	// It is not divisible by 4.
    	else {
    	    System.out.println(dYear + " is not a leap year.");
    
    		}
    		
    	}
    }
    This is the block I'm specifically having a problem with setting the year validity
    I'm not getting any compiler errors, but I'm fairly certain I'm not going about it right.

    Java Code:
    public void setDate(int month, int day, int year)
       	{
    		dMonth = month;
    	  	dDay = day;
    		dYear = year;
    			
    		if(1 <= day && day <= 31)
    			dDay = day;
    		else
    			dDay = 0;
    			
    		if(1 <= month && month <= 12)
    			dMonth = month;
    		else
    			dMonth = 0;
    			
    		if(0001 <= year && year <= 2009)
    			dYear = year;
    		else
    			dYear = 0;
    				
    	}
    Hopefully someone can help me understand this a bit better. In my mind i know I have to set it so the user can not enter more than 4 integers for the year, however I'm at a loss on what to do there. The program to test this basically asks the user to input a day/month/year respectively then output it 01-01-2009 for example and say if 2009 is a leap year or not. After I get it working correctly, I will be putting in printouts to tell the user if they entered an invalid day/month/year.
    Last edited by SMHouston; 09-10-2009 at 10:25 PM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    1.) You can lose the first 3 assignments in your setDate method. They get immediately overwritten.
    2.) A method that checks whether a year is a leap year or not should take an integer (the year) and return a boolean. true if the year is a leap year and false otherwise. You could then test that leap year method separately from other parts of your program.
    I would have one liner
    Java Code:
    return ( ( ( year % 4 ) == 0 ) && ( ( year % 100 ) != 0 ) || ( ( year % 400 ) == 0 ) );

  3. #3
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Ok, so I can dump the dYear = year etc. Thanks.

    For the leap year your suggesting this?

    Java Code:
    public void isLeapYear(int dYear)
    {
         return ( ( ( year % 4 ) == 0 ) && ( ( year % 100 ) != 0 ) || ( ( year % 400 ) == 0 ) );	
    }
    Instead of the huge mess of code right?

    Also, is the setDate setup right though? I think my validity check for my year is wrong and not sure where to go with it cause I'm not getting any compiler errors but something just doesn't seem right.
    Last edited by SMHouston; 09-10-2009 at 10:31 PM.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Well your setDate doesn't check if the day is valid for the supplied month e.g 31 for February on the Gregorian calendar system.
    Your isLeapYear method won't compile like that but will find that out soon enough.

  5. #5
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    yeah, I'll have to look up the Gregorian calendar as both books I'm using to learn this stuff have nothing in them on the subject but I have run into a few example problems floating around on the net, so I figured I didn't have to bother with the calendar part for this problem in the book.

    I got rid of the 3 declarations you told me to.

    I also set the year from 0001 - 9999 instead of maxing it out at 2009.

    Gonna play with the isLeapYear method now and see what I can come up with.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Quote Originally Posted by SMHouston View Post
    I also set the year from 0001 - 9999 instead of maxing it out at 2009.
    0001 is actually 1 in octal...which is OK, since it's still 1, but you may as well simply put in 1. The leading zeroes are completely unecessary.

  7. #7
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Ok, I changed up a few things that have been suggested. Now, I have a question about the Gregorian calendar. I've read a few class data sheets on it etc, but have no idea on how to actually implement it into my class, or does that actually go into the test problem itself. As both my books don't have a single thing on the Gregorian calendar.

    Here's my code again:

    Java Code:
    public class Date
    {
       private int dMonth;		//variable to store the month
       private int dDay;		//variable to store the day
       private int dYear;		//variable to store the year
    
    
     		//Default constructor
    		//Data members dMonth, dDay, and dYear are set to
    		//the default values
    		//Postcondition: dMonth = 1; dDay = 1; dYear = 1900;
       	public Date()
       	{
    	  	dMonth = 1;
    	  	dDay = 1;
    	  	dYear = 1900;
       	}
    
    		//Constructor to set the date
    		//Data members dMonth, dDay, and dYear are set
          	//according to the parameters
    		//Postcondition: dMonth = month; dDay = day;
          	//               dYear = year;
       	public Date(int month, int day, int year)
       	{
    	  	dMonth = month;
    	  	dDay = day;
    	  	dYear = year;
       	}
    
    		//Method to set the date
    		//Data members dMonth, dDay, and dYear are set
    		//according to the parameters
    		//Postcondition: dMonth = month; dDay = day;
    		// 			 dYear = year;
       	public void setDate(int month, int day, int year)
       	{
    					
    		if(1 <= day && day <= 31)
    			dDay = day;
    		else
    			dDay = 0;
    		
    		if(1 <= month && month <= 12)
    			dMonth = month;
    		else
    			dMonth = 0;
    			
    		if(1 <= year && year <= 9999)
    			dYear = year;
    		else
    			dYear = 0;
    				
    	}
    
     		//Method to return the month
     		//Postcondition: The value of dMonth is returned
       	public int getMonth()
       	{
     	  	return dMonth;
       	}
    
    		//Method to return the day
    		//Postcondition: The value of dDay is returned
       	public int getDay()
       	{
     	  	return dDay;
       	}
    
    		//Method to return the year
    		//Postcondition: The value of dYear is returned
       	public int getYear()
       	{
     	  	return dYear;
       	}
    
    		//Method to return the date in the form mm-dd-yyyy
       	public String toString()
       	{
    	 	return (dMonth + "-" + dDay + "-" + dYear);
       	}
    		
    		//Method to check if the year is a leap year
    	public boolean isLeapYear(int dYear)
    	{
    		return ( ( ( dYear % 4 ) == 0 ) && ( ( dYear % 100 ) != 0 ) || ( ( dYear % 400 ) == 0 ) );
    	}	
    }

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Don't worry if your books don't have anything about it. You can read all about it here

  9. #9
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Ok, I've read it a few times, still a bit confused, but I get the concept.

    Now is it possible to get user input by them entering a date, I'm not sure how to check it against the gregorian calendar. I can make a program that plops out the current date, time, and timezone but other than that I just went all flying over my head.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    There is also a method to verify leap years in there.
    You can also use SimpleDateFormat to convert a user entered string into a Date which you can then set as the Calendar's time.

  11. #11
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Ok, but that's where I get confused. Looking at the class lists like that is confusing to me, I've honestly never understood them. Like when I look at the isLeapYear one, have no idea what they mean.

    I'm a newb at JAVA trying to learn it on my own going through a couple books I picked up at the local book store that was on sale, you can probably tell.

    So please excuse me if I'm sounding way off base or just not getting it :)

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    If you are learning on your own, the first thing to do is get Sun's tutorial onto your computer. It is free and starts with the hello world program.

  13. #13
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Well I'm passed the hello world problem, I'm on chapter 9 in my book and thats where I'm starting to run into troubles. Chapter 3 in my one book is the same exact thing as chapter 9 in my beginner book with the same exact problems and thats where I'm at as I've been going through the book for awhile now.

    The advance book gives ya most of the class code then the problems want you to fill in the blanks where it asks. Trying to make heads or tails of it as, the advance book is extremely vague on a few things.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Quote Originally Posted by SMHouston View Post
    Well I'm passed the hello world problem, I'm on chapter 9 in my book and thats where I'm starting to run into troubles. Chapter 3 in my one book is the same exact thing as chapter 9 in my beginner book with the same exact problems and thats where I'm at as I've been going through the book for awhile now.

    The advance book gives ya most of the class code then the problems want you to fill in the blanks where it asks. Trying to make heads or tails of it as, the advance book is extremely vague on a few things.
    Honestly, do the Sun tutorials. They're very good and should get you through all this.

  15. #15
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Ok, here is where I'm at. No need to use the gregorian calendar at this point will work on it later.

    Clase Date:
    Java Code:
    public class Date
    {
       private int dMonth;		//variable to store the month
       private int dDay;		//variable to store the day
       private int dYear;		//variable to store the year
    
    
     		//Default constructor
    		//Data members dMonth, dDay, and dYear are set to
    		//the default values
    		//Postcondition: dMonth = 1; dDay = 1; dYear = 1900;
       	public Date()
       	{
    	  	dMonth = 1;
    	  	dDay = 1;
    	  	dYear = 1900;
       	}
    
    		//Constructor to set the date
    		//Data members dMonth, dDay, and dYear are set
          	//according to the parameters
    		//Postcondition: dMonth = month; dDay = day;
          	//               dYear = year;
       	public Date(int month, int day, int year)
       	{
    	  		dDay = day;
    			dMonth = month;
    			dYear = year;
    		}	
    		
    		//Method to set the date
    		//Data members dMonth, dDay, and dYear are set
    		//according to the parameters
    		//Postcondition: dMonth = month; dDay = day;
    		// 			 dYear = year;
       	public void setDate(int month, int day, int year)
       	{
    			if(year >= 1)
              dYear = year;
           else
              dYear = 1900;
    
           if(1 <= month && month <= 12)
              dMonth = month;
           else
            dMonth = 1;
    
           switch(dMonth)
           {
           case 1: case 3: case 5: case 7:
           case 8: case 10: case 12: if(1 <= day && day <= 31)
                                dDay = day;
                             else
                                dDay = 1;
                             break;
           case 4: case 6:
           case 9: case 11: if(1 <= day && day <= 30)
                          dDay = day;
                        else
                          dDay = 1;
                        break;
           case 2: if(isLeapYear(day))
                 {
                    if(1 <= day && day <= 29)
                       dDay = day;
                    else
                       dDay = 1;
                 }
                 else
                 {
                    if(1 <= day && day <= 28)
                       dDay = day;
                    else
                       dDay = 1;
                 }
           }	
    		}
    
     		//Method to return the month
     		//Postcondition: The value of dMonth is returned
       	public int getMonth()
       	{
     	  	return dMonth;
       	}
    
    		//Method to return the day
    		//Postcondition: The value of dDay is returned
       	public int getDay()
       	{
     	  	return dDay;
       	}
    
    		//Method to return the year
    		//Postcondition: The value of dYear is returned
       	public int getYear()
       	{
     	  	return dYear;
       	}
    
    		//Method to return the date in the form mm-dd-yyyy
       	public String toString()
       	{
    	 	return (dMonth + "-" + dDay + "-" + dYear);
       	}
    		
    		public boolean isLeapYear(int dYear)
    		{
    			if((dYear%100 != 0) || (dYear%400 == 0))
    			{
    				return true;
    			}
    			return false;
    		}
    }
    Now where I'm running into a problem is my test program. Because my setDate method is : setDate (int,int,int) i'm not sure how to go about getting the user input like that. I've been trying to have the user put in the month, set the month, put in the day, set the day but i don't think its gonna work like that.

    So I went ahead and tried to get it to read the date ie if a user enters 02/24/1991 and set it but I just can't get it. Heres the code, I've been trying to fight with, but I'm getting the incompatible types again, and not sure where to head from here.

    Java Code:
    //Program Test Dates
    
    import java.io.*;
    
    public class TestDate
    {
    	static BufferedReader keyboard =
                    new BufferedReader(new InputStreamReader(System.in));
    	public static void main(String[] args) throws IOException
    	{
    		int dDay;
    		int dMonth;
    		int dYear;
    		Date yourDate = new Date();
    				
    		System.out.println("Please enter the Date");
    		System.out.flush();
    			yourDate = keyboard.readLine();
    		System.out.println(dMonth);
    		
    		yourDate.setDate(dMonth, dDay, dYear);
    	}
    }

  16. #16
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    First decide on the requirements. What must users enter?

  17. #17
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Well, they'de be entering an integer for the month, correct?

    That way it can sort through the cases and check if the month has 30, 31, or 29 days.

    That's where I'm stuck though. If I try to do it line by line, like ask the user to enter the month first, then the day, then the year i get the (int,int,int) error.

    If I try to do it where the user enters the date i.e. 2/24/2009 I get incompatible type error because its a string value.

    Little confused.

  18. #18
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by SMHouston View Post
    Well, they'de be entering an integer for the month, correct?

    That way it can sort through the cases and check if the month has 30, 31, or 29 days.

    That's where I'm stuck though. If I try to do it line by line, like ask the user to enter the month first, then the day, then the year i get the (int,int,int) error.

    If I try to do it where the user enters the date i.e. 2/24/2009 I get incompatible type error because its a string value.

    Little confused.
    Please do yourself a favor and try to understand what those error messages mean. If you take one part of the date at a time then you must delay calling the method that takes 3 integers until you have all the integers ready. If you are taking the day as a String then you must either convert that string to the type of arguments that your method is calling or your change your method to take a string. Which way you go depends on your requirements. You shouldn't be asking us how the user must be entering the date as.
    That should be obvious from your requirements.

    The longer this threads goes the more obvious it is that you need to read Sun's tutorial.

  19. #19
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Naw, It was a dumb long day and I made alot of mistakes and my brain was farting uncontrollably, sorry about that. I figured it out and got it set up.

    I have one last question though. When I run my program everything checks out fine, except my output is acting screwy.

    Here's what I get :

    The date you entered is: 1-1-2008
    2008 is a leap year
    2008 is a leap year
    true

    Now, my question is , how do i get rid of the second printout and the true statement? if there is a way.

    Here's my isLeapYear method:

    Java Code:
    	
    public boolean isLeapYear(int dYear)
    {
    		if	( ( ( dYear % 4 ) == 0 ) && ( ( dYear % 100 ) != 0 ) || ( ( dYear % 400 ) == 0 ) )	
    		{
    			System.out.println(dYear + " is a leap year");
    			return true;
    			
    		}
    			System.out.println(dYear + " is not a leap year");
    			return false;
    }
    Here is my test program:

    Java Code:
    //Program Test Dates
    
    import java.io.*;
    
    public class TestDate
    {
    	static BufferedReader keyboard =
                    new BufferedReader(new InputStreamReader(System.in));
    	public static void main(String[] args) throws IOException
    	{
    		int dDay;
    		int dMonth;
    		int dYear;
    		Date yourDate = new Date();
    		
    			
    		System.out.println("Please enter the month");
    		System.out.flush();
    			dMonth = Integer.parseInt(keyboard.readLine());
    		
    		
    		System.out.println("Please enter the day");
    		System.out.flush();
    			dDay = Integer.parseInt(keyboard.readLine());
    		
    		
    		System.out.println("Please enter the year");
    		System.out.flush();
    			dYear = Integer.parseInt(keyboard.readLine());
    		
    		
    		yourDate.setDate(dMonth, dDay, dYear);
    		System.out.println();
    		System.out.println("The date you entered is: " + yourDate.toString());
    		
    		yourDate.isLeapYear(dYear);
    		System.out.println(yourDate.isLeapYear(dYear));
    	}  
    }
    Everything works fine except getting the double print out.

  20. #20
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Cancel that, figured it out. Had to put an if loop in my main program, thanks again for the help, and slapping me around some.

Similar Threads

  1. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-10-2009, 11:35 PM
  2. Storing Files
    By superwaxer in forum Advanced Java
    Replies: 6
    Last Post: 05-07-2009, 02:37 PM
  3. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  4. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  5. Replies: 0
    Last Post: 11-30-2007, 11:55 PM

Posting Permissions

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