Hello there java gang.

I've written a program which basically checks the dates that i have written for the program in question and then validates it against a set of rules, using arrays to ensure that what a user enters is valid.

So for example, you can have 1,(day) 5,(month), 2009(year). And this will be validated correctly, for it is within the parameters.

I'm having two problems, one is, how do i allow a user to enter a date and store it in the place i have written my other dates, USING the void main parameter entry form, (not a buffered reader) and i thought i had my validation for invalid dates correctly, but when i enter a number like -1 for the day, it breaks the program and gives me an invalid data response.

PS. I know i've done the Array's the long winded and silly way, but i'm living in the past.

public class Date /* Class of Date */
{
    private static String[] monthsArray = new String[12];

    private static int[]daysInMonthArray = new int[12];

    private static String[]dayEndings = new String[31];

    public static void main(String [] args){

        /*This is the array for the months jan-dec*/
        monthsArray[0] = "January";
        monthsArray[1] = "February";
        monthsArray[2] = "March";
        monthsArray[3] = "April";
        monthsArray[4] = "May";
        monthsArray[5] = "June";
        monthsArray[6] = "July";
        monthsArray[7] = "August";
        monthsArray[8] = "September";
        monthsArray[9] = "October";
        monthsArray[10] = "November";
        monthsArray[11] = "December";

        //This is the array used for the dates of each month from Jan-Dec//
        daysInMonthArray[0] = 31;
        daysInMonthArray[1] = 28;
        daysInMonthArray[2] = 31;
        daysInMonthArray[3] = 30;
        daysInMonthArray[4] = 31;
        daysInMonthArray[5] = 30;
        daysInMonthArray[6] = 31;
        daysInMonthArray[7] = 31;
        daysInMonthArray[8] = 30;
        daysInMonthArray[9] = 31;
        daysInMonthArray[10] = 30;
        daysInMonthArray[11] = 31;

        //This next array displays the ending of each date, eg 1st 2nd 3rd//
        dayEndings[0] = "st";
        dayEndings[1] = "nd";
        dayEndings[2] = "rd";
        dayEndings[3] = "th";
        dayEndings[4] = "th";
        dayEndings[5] = "th";
        dayEndings[6] = "th";
        dayEndings[7] = "th";
        dayEndings[8] = "th";
        dayEndings[9] = "th";
        dayEndings[10] = "th";
        dayEndings[11] = "th";
        dayEndings[12] = "th";
        dayEndings[13] = "th";
        dayEndings[14] = "th";
        dayEndings[15] = "th";
        dayEndings[16] = "th";
        dayEndings[17] = "th";
        dayEndings[18] = "th";
        dayEndings[19] = "th";
        dayEndings[20] = "st";
        dayEndings[21] = "nd";
        dayEndings[22] = "rd";
        dayEndings[23] = "th";
        dayEndings[24] = "th";
        dayEndings[25] = "th";
        dayEndings[26] = "th";
        dayEndings[27] = "th";
        dayEndings[28] = "th";
        dayEndings[29] = "th";
        dayEndings[30] = "st";  

        if (args.length==0)
        {
            System.out.println(date(12, 2, 2006));
            System.out.println(date(31, 10, 2007));
            System.out.println(date(22, 5, 2008));
            System.out.println(date(29, 2,2007));
            System.out.println(date(3, 6, 2006));
            System.out.println(date(65, 7, 2009));/* testing for totally invalid day input*/
            System.out.println(date(21, 5, 2015));/*testing for years in the future*
            /* When using date method, should read out one invalid date for february and 4 other correct dates,
             * The date method allows the computer to check and validate the dates input.
             */
        }
        else{
            try
            {
                System.out.println(date(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2])));
            }
            catch(Exception e)
            {
                System.out.println("Invalid entry, please re-iterate");
            }
        }
    }

    public static String date(int day,int month,int year){
        if (day<1 || day>daysInMonthArray[month-1])
            return "Invalid date "+day+" of month "+monthsArray[month-1];

        else if (month<1 || month>12)
            return"Invalid month "+month;

        else if (year<0)
            return "Invalid year "+year;
        return ""+monthsArray[month-1]+" "+day+dayEndings[day-1]+", "+year;
    }

}

Recommended Answers

All 8 Replies

Line 98 you use month to index an array, but you don't validate month until line 101. So an invalid month is going to throw an exception on line 98. You need to validate month first.
Around line 92 you should print the exception e to see exactly what went wrong and where - you may be surprised.

Ahh thank you, sometimes i forget how Java works in steps, i'll have a quick change and see how it rolls.

public static String date(int day,int month,int year){
        if (day<1 || day>daysInMonthArray[month-1])
            return "Invalid date "+day+" of month "+monthsArray[month-1];

        else if (month<1 || month>12)
            return"Invalid month "+month;

        else if (year<0)
            return "Invalid year "+year;
        return ""+monthsArray[month-1]+" "+day+dayEndings[day-1]+", "+year;
    }

Hold on a second, if i was to validate month first, then wouldn't it find an exception for day instead?

Problem is: if month is invalid your array index daysInMonthArray[month-1] will be invalid, so the JRE will throw an unchecked runtime exception and your date method will terminate abnormally without returning any kind of explanation message. If you validate month first it will return "Invalid month "+month, which is correct behaviour.

Ok i've fixed that now, and it works, your solution was correct sorry for doubting :P, now all i need to do is input a date in the form of DD/MM/YYYY, without using a buffered reader and using the method call entry parameters, any idea how i can go about this using that code? I've already started the error function and invalid response, but i can't seem to get an input out of it using "DD,MM,YYYY", which i'm pretty sure is what it's meant to be like from scratch.

I'm not sure what you are trying to do there. You currently seem to accept three runtime arguments and parse those correctly as a date, yes?
Now you want to pass a single argument that is a String in format DD/MM/YYYY or DD,MM,YYYY, yes?
Step 1 - can you pass a string in that kind of format and receive it in args[0] (try printing it)?

Basically, i'm trying to get a user to type in a date in the format of DD-MM-YY, i have a exception handler in place, but i keep getting an Number format exception,

if (args.length==0)
        {
            System.out.println(date(12, 2, 2006));
            System.out.println(date(31, 10, 2007));
            System.out.println(date(22, 5, 2008));
            System.out.println(date(29, 2,2007));
            System.out.println(date(3, 6, 2006));
            System.out.println(date(65, 7, 2009));/* testing for totally invalid day input*/
            System.out.println(date(21, 5, 2015));/*testing for years in the future*
            /* When using date method, should read out one invalid date for february and 4 other correct dates,
             * The date method allows the computer to check and validate the dates input.
             */
        }
        else{
            try
            {
                System.out.println(date(Integer.parseInt(args[0]),Integer.parseInt(args[1]),Integer.parseInt(args[2])));
            }
            catch(Exception e)
            {
                System.out.println("Invalid entry, please re-iterate");
            }
        }

It's this part i'm using to weigh up the arguments, it should be parsing already but it's having slight difficulty and i'm not sure why! :( Thank you so much for your help so far, it's so easy to overlook things and you've been a real star.

Like I said earlier:
Around line 92 you should print the exception e to see exactly what went wrong and where
can you pass a string in that kind of format and receive it in args[0] (try printing it)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.