Results 1 to 20 of 20
- 09-10-2009, 10:08 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
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:
This is the block I'm specifically having a problem with setting the year validityJava 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."); } } }
I'm not getting any compiler errors, but I'm fairly certain I'm not going about it right.
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.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; }Last edited by SMHouston; 09-10-2009 at 10:25 PM.
- 09-10-2009, 10:23 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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 ) );
- 09-10-2009, 10:28 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
Ok, so I can dump the dYear = year etc. Thanks.
For the leap year your suggesting this?
Instead of the huge mess of code right?Java Code:public void isLeapYear(int dYear) { return ( ( ( year % 4 ) == 0 ) && ( ( year % 100 ) != 0 ) || ( ( year % 400 ) == 0 ) ); }
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.
- 09-10-2009, 10:37 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 09-10-2009, 10:53 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
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.
- 09-11-2009, 09:51 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 09-11-2009, 08:09 PM #7
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
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 ) ); } }
- 09-11-2009, 09:00 PM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Don't worry if your books don't have anything about it. You can read all about it here
- 09-11-2009, 09:25 PM #9
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
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.
- 09-11-2009, 09:58 PM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 09-12-2009, 12:43 AM #11
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
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 :)
- 09-12-2009, 08:24 AM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 09-12-2009, 03:09 PM #13
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
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.
- 09-14-2009, 12:26 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 09-19-2009, 06:19 PM #15
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
Ok, here is where I'm at. No need to use the gregorian calendar at this point will work on it later.
Clase Date:
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.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; } }
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); } }
- 09-19-2009, 08:09 PM #16
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
First decide on the requirements. What must users enter?
- 09-20-2009, 03:37 PM #17
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
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.
- 09-20-2009, 04:58 PM #18
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 09-21-2009, 10:43 PM #19
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
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:
Here is my test program: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; }
Everything works fine except getting the double print out.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)); } }
- 09-21-2009, 10:54 PM #20
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
Similar Threads
-
HashMap contains all values but doesn't show all values
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 05-10-2009, 11:35 PM -
Storing Files
By superwaxer in forum Advanced JavaReplies: 6Last Post: 05-07-2009, 02:37 PM -
Retaining DB values as well as Dynamically generated Values.. Help Needed !
By rajivjha in forum Advanced JavaReplies: 0Last Post: 05-22-2008, 10:53 AM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM -
How to add interceptor to persistent ejb entity to add check before returning values
By umen in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 11-30-2007, 11:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks