Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Networking TechnologyRSS Feeds

Define an array of ten String elements each containing an arbitrary string of the form “month/day/year”

Posted By: Lurlene Fischer     Category: Java     Views: 2915

Define an array of ten String elements each containing an arbitrary string of the form “month/day/year”; for example,”8/2/06” or “12/5/99”.Analyze each element in the array and output the date represented in the form 2nd August 2006. If the year is from 50 to 99 append “19” string and if year is from 01 to 49 append “20” string to the year.Also put the validation for the Date and generate a Date Exception.

Code for Define an array of ten String elements each containing an arbitrary string of the form “month/day/year” in Java

class MyException extends Exception
{
    privateint detail;
    private String str;
    
    MyException(int d,String s)
    {
        detail=d;
        str=s;
    }
    public String toString()
    {
        return str+" MyException["+detail+"]";
    }
}
class checkDate
{
    String arrD[]=new String [10];
    int month,day,year,d;
    
    staticint i=-1;

    checkDate(String a[])
    {
        arrD=a;
    }
    
    void check() throws MyException
    {
        String s;
        
        i++;
        System.out.println(arrD[i]);

        //Check month

        s=arrD[i].substring(0,2);
        month=Integer.parseInt(s);
        
        if(month>12)
            thrownew MyException(month,"month");
        
        //Check day and year

        s=arrD[i].substring(3,5);
        day=Integer.parseInt(s);
        
        s=arrD[i].substring(6,8);
        year=Integer.parseInt(s);

        if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
            d=31;
        elseif(month==4 || month==6 || month==9 || month==11)
            d=30;
        else
        {
            if(year%4==0)
                d=29;
            else
                d=28;
        }

        if(1>day || day > d)
            thrownew MyException(day,"day");        
    }
    void format()
    {
        switch(month)
        {
            case 1:
                System.out.print(day+" January ");
                break;
            case 2:
                System.out.print(day+" February ");
                break;
            case 3:
                System.out.print(day+" March ");
                break;
            case 4:
                System.out.print(day+" April ");
                break;
            case 5:
                System.out.print(day+" May ");
                break;
            case 6:
                System.out.print(day+" Jun ");
                break;
            case 7:
                System.out.print(day+" July ");
                break;
            case 8:
                System.out.print(day+" August ");
                break;
            case 9:
                System.out.print(day+" September ");
                break;
            case 10:
                System.out.print(day+" October ");
                break;
            case 11:
                System.out.print(day+" Nevember ");
                break;
            case 12:
                System.out.print(day+" Disember ");
                break;
        }
        if(year >= 50 && year <= 99)
            System.out.print("19"+year+"\n");
        elseif(year >= 1 && year <= 49)
        {
            if(year >= 1 && year <= 9)
                System.out.print("200"+year+"\n");
            else
                System.out.print("20"+year+"\n");
        }

    }
}


class Date
{
    publicstaticvoid main(String args[])
    {
        String arrD[]=new String [10];
        arrD[0]="04/31/83";
        arrD[1]="05/02/09";
        arrD[2]="02/28/54";
        arrD[3]="02/29/92";
        arrD[4]="02/31/90";
        arrD[5]="30/10/06";
        arrD[6]="02/31/55";
        arrD[7]="08/31/80";
        arrD[8]="02/29/54";
        arrD[9]="12/12/03";
    
        checkDate d=new checkDate(arrD);
        
        System.out.println("----------------------------------");
        for(int k=0;k<10;k++)
        {
            try
            {
                d.check();
                d.format();
            }catch(MyException e)
            {
                System.out.println("  Invalid "+e);
            }

            System.out.println("----------------------------------");
        }    
    }
}
/*
Output

----------------------------------
04/31/83
Invalid day MyException[31]
----------------------------------
05/02/09
2 May 2009
----------------------------------
02/28/54
28 February 1954
----------------------------------
02/29/92
29 February 1992
----------------------------------
02/31/90
Invalid day MyException[31]
----------------------------------
30/10/06
Invalid month MyException[30]
----------------------------------
02/31/55
Invalid day MyException[31]
----------------------------------
08/31/80
31 August 1980
----------------------------------
02/29/54
Invalid day MyException[29]
----------------------------------
12/12/03
12 Disember 2003
----------------------------------

*/
  
Share: 



Lurlene Fischer
Lurlene Fischer author of Define an array of ten String elements each containing an arbitrary string of the form “month/day/year” is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!