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;
}
}