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.
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!
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 ]
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 ]
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???
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.
Apologies for my miss use of the wordcast. 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 ]
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.
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.
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
Post by:autobot
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
a bit of art, as a gift, that will fit in a stocking