File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes validating a range of values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "validating a range of values" Watch "validating a range of values" New topic
Author

validating a range of values

Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
Hi
I have date in the type string as in "0195" where the first two being the month and the last two being year (MMYY) i need to validate this date againt a range say 01/80 through to 12/10 and also test that the month is valid and the year is valid. What i have been doing is parsing the substring of the character as an Integer and assigning the values to variables month and year. that's seems fine but when i test for say the year if i have say 81 as in 1981
the test fails because 81 is > 10, appriciate any help thanks


Martyn...<br /> <br />SCJP 1.4 SCWCD 1.4
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
You have to decide what year a two digit date actually represents.
If 10 represents 2010 and 81 represents 1981 then you have to add 2000 to the 10 and 1900 to the 81 and then compare them.


Joanne
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Assuming you know for sure that the year comes from parsing just two characters, then you know it's a number between 0 and 99, inclusive. So you want to say that the year is 1980 or later, or 2010 or earlier; this might look like this:

if (year >= 80 || year <= 10) ...


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
We had to do some of this guessing during the Y2K panic.

Then you could create date objects (assume dd = 01) and compare to your max & min dates. I'd do that to make sure I caught 0113 and 8900 as errors.


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

Joined: Apr 16, 2005
Posts: 108
Thank's all,
play's about with your examples and it now works fine.
Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
Thought i had this sussed!
Still have a problem validating past 2000
what i have is the string value being passed in this format MMYY as in "0785"
now in the method i have:
someMethod(String date)
{
int month=0;
int year=0;
if(date.length()== 4)
{
month = Integer.parseInt(date.substring(0, 2));
year = Integer.parseInt(date.substring(2, 4));
if(year > 99)
{
year = year + 2000;
}
else
{
year = year + 1900;
}
if (month < 1 || month > 12)
{
System.out.println("Invalid Month entered.");
}
if (year < 1981 || year > 2020)
{
System.out.println("Invalid year entered");
}
}
else
{
System.out.println("You must enter in the format MMYY");
}
}

the problem is it works one way but not the other? the string is only allowed to be 4 digits so i only have 2 for the year. I can not seem to get it right. Any help please.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

if(year > 99)
{
year = year + 2000;
}
else
{
year = year + 1900;
}

A 2-digit year will never be greater than 99, right? In Stan's example, "cutoff" is some number like 20, so you get years from 1921-2020.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

A 2-digit year will never be greater than 99, right?

It might be, for very large values of 2.


Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Oops, sorry I didn't describe the cutoff trick better, though I did mention it was a "guess". I can't remember what kind of dates we were dealing with that we thought we could guess with confidence. Your range sounds limited enough that you can, though.
Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
Hi all,
ended up with this and it seems to work fine. My problem seemed to be i had the test the wrong way round. I now have:
if(year >=80 && year <= 99)
{
year = year + 1900;
}
else
{
year = year + 2000;
}

Once again thanks for you help...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validating a range of values
 
Similar Threads
Passed with score 81%
i've passed the SCJP 6.0 with 81%
Passed 141 - 81%
to fetch multiple ids
Passed (81%) & My Notes