Had an assigment making a bankaccount, and validating the account owners age, this is just a bit of the code.....
public boolean checkDate(String myDate) // my date should be in the format 20111224
{
int year = Integer.parseInt(myDate.substring(0, 4));// convert the year 2011 to int
int month = Integer.parseInt(myDate.substring(4,6)); //convert the month 12 to int
int day = Integer.parseInt(myDate.substring(6)); // convert the day 24 to int
if (year >=1900 && year < 2012) //test of the year
{
System.out.println("Year is ok");
if (month >= 1 && month <= 12)
{
system.out.println("Month is OK");
}
if ((month = 1)&&(day >= 1 && day <=31))
{
System.out.println("Day is OK");
}
if ((month = 2)&&(day >= 1 && day <=29))
{
System.out.println("Day is OK");
}
if ((month = 3)&&(day >= 1 && day <=31))
{
System.out.println("Day is OK");
}
if ((month = 4)&&(day >= 1 && day <=30))
{
System.out.println("Day is OK");
}
if ((month = 5)&&(day >= 1 && day <=31))
{
System.out.println("Day is OK");
}
if ((month = 6)&&(day >= 1 && day <=30))
{
System.out.println("Day is OK");
}
if ((month = 7)&&(day >= 1 && day <=31))
{
System.out.println("Day is OK");
}
if ((month = 8)&&(day >= 1 && day <=31))
{
System.out.println("Day is OK");
}
if ((month = 9)&&(day >= 1 && day <=30))
{
System.out.println("Day is OK");
}
if ((month = 10)&&(day >= 1 && day <=31))
{
System.out.println("Day is OK");
}
if ((month = 11)&&(day >= 1 && day <=30))
{
System.out.println("Day is OK");
}
if ((month = 12)&&(day >= 1 && day <=31))
{
System.out.println("Day is OK");
return true;
}
Question: there must be a easyer way to validate that month and day match. Examble Febuary 29 or June 30 is true and june 31 and febuary 31 is false. Leape year not countet.
Only been programming for 8weeks, so pls dont yell at me, if i seem like an airhead for not understanding this. It just seem strange that i had to make 12 'ifs' there must be a simple way, an algoritme for this problem.
i was thinking something like:
if (month == 1,3,5,7,8,10,12) && (day >=1 && day<=31); return true;
an so on..... Why not?? remember im a noob