Win a copy of Learning Regular Expressions this week in the General Computing forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Liutauras Vilda
• Campbell Ritchie
• Tim Cooke
• Bear Bibeault
• Devaka Cooray
Sheriffs:
• Jeanne Boyarsky
• Knute Snortum
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Ganesh Patekar
• Stephan van Hulst
• Pete Letkeman
• Carey Brown
Bartenders:
• Tim Holloway
• Ron McLeod
• Vijitha Kumara

# Using an int to hold a four digit date

Greenhorn
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

author
Ranch Hand
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

Ranch Hand
Posts: 704
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 ]

author
Sheriff
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 ]

(instanceof Sidekick)
Ranch Hand
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 ]

aj woodier
Greenhorn
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?

Ranch Hand
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.

Nigel Browne
Ranch Hand
Posts: 704

Originally posted by Nigel Brown:

Originally posted by Ilja Preuss:

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

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
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.

Ranch Hand
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!

Ranch Hand
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

aj woodier
Greenhorn
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

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?