This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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 ]
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
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
Joined: Jul 27, 2004
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???
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 ]
Joined: Jul 27, 2004
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.
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.