public static void main(String[] args) {
// TODO Auto-generated method
stub
Scanner keyboard = new
Scanner(System.in);
System.out.println("Enter a date in
the format month/day/year");
String date =
keyboard.nextLine();
//Make a copy
String dateCopy = date;
//Extract the values
//start with month
//indexOf() is used to find the
index of a specified character in a givenString
int workingIndex =
date.indexOf("/");
//substring(int beginIndex, int
endIndex)
//return strings substring
String sM =
date.substring(0,workingIndex);
//convert string to integer
int m = Integer.parseInt(sM);
//move to day
date =
date.substring(workingIndex+1);
workingIndex =
date.indexOf("/");
String sD =
date.substring(0,workingIndex);
int d = Integer.parseInt(sD);
//move to year
date =
date.substring(workingIndex+1);
int year =
Integer.parseInt(date);
//check the valid date
//a valid month must be from 1 to
12
if(m<1 || m>12)
{
System.out.println(dateCopy + "is invalid! The month must be
between 1 and 12.");
}
//Leap year
//February has 28 days except on
leap years (29).
else if (m==2)
{
if(d ==
29)
{
//Leap years are years that are divisible by
4.
//but not divisible by 100 unless also divisible
by 400.
if(!(year%4==0 && (year%100!=0 ||
year%400 == 0)))
{
System.out.println(dateCopy +
" is invalid! The date given is not a leap year.");
}
else
{
System.out.println(dateCopy +
" is a valid date!");
}
}//Normal
year
else if (d<1
|| d>28)
{
System.out.println(dateCopy + " is invalid! The
day is wrong for this month. Must be between 1 and 28.");
}
else
{
System.out.println(dateCopy + " is a valid
date!");
}
}
//April, June, September, and
November have 30 days
else if(m==4 && (d<1 ||
d>30) || m==6 && (d<1 || d>30) || m==9 &&
(d<1 || d>30) || m==11 && (d<1 || d>30))
{
System.out.println(dateCopy + " is invalid! The day is wrong for
this month. Must be between 1 and 30.");
}
else if((m==1 || m==3 || m==5 ||
m==7 || m==8 || m==10 || m==12) && (d<1 ||
d>31))
{
System.out.println(dateCopy + " is invalid! The day is wrong for
this month. Must be between 1 and 31.");
}
else
{
System.out.println(" is a valid date!");
}
}
}
Looking for the flowchart for this code. It determines the validity of a user inputted birth date entered in the format in the code.
The Flowchart is very big.
Please find all the conditions slowly.
I have made it easy for you to understand.
Get Answers For Free
Most questions answered within 1 hours.