Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: help please...

  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Thanked 0 Times in 0 Posts

    help please...

    Write a boolean method isValid that returns a true if an inputted date is valid, or false in case it is not.

    class dateValidity {

    int d;
    int m;
    int y;
    boolean valid;

    boolean isValid(int d, int m, int y) {
    if (d == 12 && m == 12 && y == 2004)
    valid = true;
    else valid = false;
    return (valid);

    public static void main (String[] args) {
    isValid s = new isValid();

    System.out.print ("Enter day number: ");
    int d = Keyboard.readInt();

    System.out.print ("Enter month number: ");
    int m = Keyboard.readInt();

    System.out.print ("Enter year number: ");
    int y = Keyboard.readInt();

    System.out.print(s.isValid(d, m, y)); //this is not working...

    Can someone help me out with this < System.out.print(s.isValid(d, m, y)); > please? I am new to Java. Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,659 Times in 2,628 Posts
    Simple fix: isValid s = new isValid();. You don't have an object called isValid, you have an object called dateValidity. Change it to a new dateValidity and you can then access the s.isValid.

    Depending on you're rules (and the use of this validator) depends on if it will work though. This will only work if the d/m/y provided is 12/12/2004, otherwise it will fail.
    If you're looking to validate if a provided date is actually a date, its a little trickier. You need to determine how many days are available in a month (30 days in September, April, June and November, 31 in all others except for February with 28). Then you need to determine if its a leap year to make February 29 days (google up leapyear calculations, its one line of code). Switch that up with you're provided month and you can check to make sure you're input days is <= the total available days.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    alien2000 (11-14-2008)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts