-3

The question is as follows: Define a class MyDate (day, month, year) with methods to accept and display a MyDate object. Accept date as dd, mm, yyyy. Throw user defined exception "InvalidDateException" if the date is invalid. Examples of invalid dates:

12 15 2015
31 6 1990
29 2 2001

I wrote the following code but its not working in some instances. Please assist!

import java.util.*;
import java.util.Arrays;
import java.io.*;

class InvalidDateException extends Exception
{}

class MyDate 
{
    int day,month,year;
    
    void accept()
    {   
        try
        {  
            Scanner S= new Scanner(System.in);
            int d,m,y;
            System.out.println("\nEnter day \nExample: 12 25 30  =>");
            d=S.nextInt();
            System.out.println("\nEnter month \nExample: 2 6 12  =>");
            m=S.nextInt();
            System.out.println("\nEnter year \nExample: 2012 2025 1930  =>");
            y=S.nextInt();
            //display();                                 for testing porposes
            //System.out.println(valid(d,m,y));          for testing porposes
            if(valid(d,m,y))
            {
                day=d;
                month=m;
                year=y;
            }
            else
            {
                throw new InvalidDateException();
            }
         }
        catch(Exception e)
        {
            System.out.println("InvalidDateException");
        }
    }
    
    boolean valid(int d,int m,int y)
    {
        int []m31=new int[]{1,3,5,7,8,10,12};
        int []m30=new int[]{4,6,9,11};
                //System.out.println("\n\t\t"+d+m+y);            for testing porposes
        if(Arrays.asList(m31).contains(m) && d<=31)
        {
            
            return true;
        }
        else if(Arrays.asList(m30).contains(m) && d<=30)
        {
            return true;
        }
        else if(y%4==0 && d<=29)
        {
            return true;
        }
        else if(y%4!=0 && d<=28)
        {
            return true;
        }
        else
        {
            return false;
        }
        
        
        
        
    }
    
    void display()
    {
        System.out.println("DAY-MONTH-YEAR :: "+day+"-"+month+"-"+year);
    }
    
}


class Main
{
    public static void main(String args[])
    {
        MyDate date= new MyDate();
        date.accept();
        date.display();
    }
}

While I have resolved the other issues, the program seems to not work for days above 29.

Its showing valid if day is below 30

And invalid for day above 29 changing month or year has no affect

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • 3
    I notice you're catching all exceptions - are you sure it's an `InvalidDateException` that's getting thrown? In general `catch(Exception E)` is a dangerous line of code to write, because it can obfuscate unexpected errors – Edward Peters Nov 24 '22 at 01:17
  • Yeap, just comment out the try/catch block and let it explode, see what is the error :) and if you don't figure out post the stack trace here in your question – Jorge Campos Nov 24 '22 at 01:18
  • Could you, please, include the sample runs as text in the question body? You may edit the question to do that. – Old Dog Programmer Nov 24 '22 at 01:20
  • 2
    Make your life easier: `List m31 = List.of(1,3,5,7,8,10,12);` then you can `if (m31.contains(m))` – Bohemian Nov 24 '22 at 01:26
  • Since `m31` and `m30` have elements in ascending order, can `Arrays.binarySearch(m31, m) >= 0` be substituted for `Arrays.asList(m31).contains(m)` ? – Old Dog Programmer Nov 24 '22 at 01:43
  • there was another instance that came across when I was making changes: " 12 13 2000" this would've returned true as no condition was placed in else if(y%4==0 && d<=29) thus the appropriate change for it was else if(m==2 && y%4==0 && d<=29) same for else if(y%4!=0 && d<=28) => else if(m==2 && y%4!=0 && d<=28) – 02 Ankesh Agarwal Nov 24 '22 at 03:09

1 Answers1

0

Arrays.asList(m31) is a list of one array, not an array converted to a list. (I hate it too.)

I'm not entirely clear why, but List<T>::contains takes an Object, not a T, so this type checked. Really seems like it shouldn't have.

It's a type inferenence thing, I think - if you just declare your arrays as Integer[] rather than int[] it works.

See Converting array to list in Java for more info.

Edward Peters
  • 3,623
  • 2
  • 16
  • 39