| Author |
Using an int to hold a four digit date
|
aj woodier
Greenhorn
Joined: Jul 27, 2004
Posts: 9
|
|
For an assignment I am currently working through I have to create a validation method that checks a four digit integer rests within 0181 --> 1220 where the first two digits represent the month and the second two digits represent the year. My problem lies with how I can store a number such as 0299 within an integer. I'm not asking for a solution to the problem here more a gentle push in the right direction of where I can find some info to help me on my way. Thanks
|
SCJP 1.4<br />SCWCD (in progress)
|
 |
Peter van der Linden
author
Ranch Hand
Joined: Sep 28, 2004
Posts: 46
|
|
0181 --> 1220 where the first two digits represent the month and the second two digits represent the year. how I can store a number such as 0299 within an integer?
Well, leading zeroes are only meaningful in Strings. So you're range is actually 181 .. 1220 and ints can hold those without any problems. A much bigger problem for you is that consecutive dates, like Dec 2003 and Jan 2004 are NOT consecutive numbers ( 1203 and 104, respectively). This is a horrible way to code dates, and I am sure the instructor set the problem partly with that in mind to see how you tackle it. You can read the straight story on all types in Java in my text Just Java. It's important to get a good understanding of basic types and what they do, so I wrote that down in some detail. Cheers aj, you are going to have a lot of fun learning and programming Java! Peter
|
Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0131482114/ref=jranch-20" target="_blank" rel="nofollow">Just Java(TM) 2 (6th Edition)</a>
|
 |
Nigel Browne
Ranch Hand
Joined: May 15, 2001
Posts: 673
|
|
How does the user enter the date/integer ? If it is as a commandline argument e.g Then you will have to parse the String into an Integer While you have a String literal it is easy to split into its two components and test that the month part falls within 1 to 12 and then you can check that the year part falls within your set range. Once both tests are passed store the input data as an int. [ October 06, 2004: Message edited by: Nigel Browne ]
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by Nigel Browne: Then you will have to cast the String into an Integer
That's not casting. Casting is the use of the cast operator: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#238146 [ October 01, 2004: Message edited by: Ilja Preuss ]
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
Storing date this way reminds me strongly of my COBOL days. One of the coolest things about COBOL is that you can define data at different levels so it's easy to retrieve or set the month or day as digits in a larger number. It just doesn't strike me as a very good thing to do in Java, tho. A friend once wrote a date manipulation program with data like this. To validate a date he had a table (array) with 1231 entries. Entry 101 thru 131 were good, entry 132 through 200 were not. Given a month & day he could validate it with a single index lookup in a table, plus an extra test for 229. And guess how he determined leap years? He had pages and pages and pages of hand coded data, but it ran too fast to measure. Fortunately I've never seen anything like it since.  [ October 01, 2004: Message edited by: Stan James ]
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
aj woodier
Greenhorn
Joined: Jul 27, 2004
Posts: 9
|
|
Thanks for the responces!! The scenario is that I have written a few classes that have to run with a larger program. I have next to finished the assignment except for this one damn method. The integer date is input into a test program, along with some other info which happen to be strings, to a create an instance of a class which will then be stored in a treemap. Within the class of the instance is a number of methods for validating certain bits of data before allowing it to be stored in the treemap. The data must be input as an integer but as I seem to be finding out this is an extremely messy way of trying to do this. If the date was a string it would be a sintch but the laws that be tell me it has to be an integer. How can I do anything with 0299 as an integer, my net beans wont even compile with this data in a simple program. When it does allow a 0 at the start of the integer the compiler seems to drop this 0 then convert the number to octal form??? What the hell is going on??? Is there any hope for me and this assignment?
|
 |
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
|
|
An integer literal is octal if preceded by a zero. However, only the digits 0 to 7 can exist in octal, so trying to have an integer literal like 0299 is a compile-time error. As a previous respondent said, you simply can't have leading zeros in integers, so you are dealing with integers like 181 (meaning January 1981) and 1220 (meaning December 2020). I don't normally like to answer homework questions, but you do seem to have made a genuine effort. If I understand the assignment correctly: - To get the year out of such an integer, you need to get the remainder after dividing by 100. That is, myYear = (myInt % 100). To validate the year, you want to check whether it is either in the range 0 to 20 or in the range 81 to 99. Any other value of year is bad. To get the month, you take the result of dividing the integer by 100. That is myMonth = (myInt / 100). To validate the month, you want to check if it is in the range 1 to 12.
|
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
|
 |
Nigel Browne
Ranch Hand
Joined: May 15, 2001
Posts: 673
|
|
Originally posted by Nigel Brown:
Apologies for my miss use of the word cast. I should of course have used the word parse and I have corrected my original post occordingly. [ October 06, 2004: Message edited by: Nigel Browne ]
|
 |
aj woodier
Greenhorn
Joined: Jul 27, 2004
Posts: 9
|
|
Once again thankyou all for the responces. I have just spoken to my tutor and it appears the assignment has wrongly specified the use of an integer and that a string can instead be used to hold the date information. As you will know this will allow me to easily validate the date in a method using numerous ways.
|
 |
Chengwei Lee
Ranch Hand
Joined: Apr 02, 2004
Posts: 884
|
|
Hi folks, I know the instructor had changed the type to String, but anyway, I thought of a solution to tackle the original isue when the type is an int. By using integer divide by 100, I should get the first 2 digits (my month) & by taking modulus 100, I should get the last 2 digits (my year). Then applying range checking, I can verify if the date fits into the range. Cheers!
|
SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
I agree with Cheng Wei Lee. Using division (/) and modulus (%) should make it rather straightforward to separate the month and date in order to do the comparison. Layne
|
Java API Documentation
The Java Tutorial
|
 |
aj woodier
Greenhorn
Joined: Jul 27, 2004
Posts: 9
|
|
The original problem I needed to deal with was that I had to fit a date such as 0399 into an integer. It seems that from the responces here and the conversation I had with the tutor last night that this is impossible therefore making the assignment specification wrong. I think the assignment wanted the programmer to use the modulus and divide operators to seperate the month from the year for verification. I think the assignment question was written but never checked with dates having a leading 0 and numbers larger than 7 following it. Thanks for all the help
|
 |
 |
|
|
subject: Using an int to hold a four digit date
|
|
|