
My program is about age calculation. It is running successfully, but once i get the output its not go to the next, its just come out from the running condition. what will i do for this. I pasted my program below.
import java.util.*;
import java.io.*;
public class AGECALC {
public static void main(String[] args) throws IOException {
int day = 1, month = 0, year = 1, ageYears, ageMonths, ageDays;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Calendar cd = Calendar.getInstance();
try {
System.out.print("Enter year of your date of birth : ");
year = Integer.parseInt(in.readLine());
{
if (year > cd.get(Calendar.YEAR)) {
System.out.print("Invalid date of birth.");
System.exit(0);
}
System.out.print("Enter month of your date of birth : ");
month = Integer.parseInt(in.readLine());
if (month < 1 || month > 12) {
System.out.print("Please enter month between 1 to 12.");
System.exit(0);
} else {
month--;
if (year == cd.get(Calendar.YEAR)) {
if (month > cd.get(Calendar.MONTH)) {
System.out.print("Invalid month!");
System.exit(0);
}
}
}
System.out.print("Enter day of your date of birth : ");
day = Integer.parseInt(in.readLine());
if (month == 1 || month == 3 || month == 5 || month == 7
|| month == 8 || month == 10 || month == 12) {
if (day > 31 || day < 1) {
System.out.print("Please enter day between 1 to 31.");
System.exit(0);
}
} else if (month == 4 || month == 6 || month == 9
|| month == 11) {
if (day > 30 || day < 1) {
System.out.print("Please enter daybetween 1 to 30.");
System.exit(0);
}
} else {
if (new GregorianCalendar().isLeapYear(year)) {
if (day < 1 || day > 29) {
System.out
.print("Please enter day between 1 to 29.");
System.exit(0);
}
} else if (day < 1 || day > 28) {
System.out.print("Please enter day between 1 to 28.");
System.exit(0);
}
}
if (year == cd.get(Calendar.YEAR)) {
if (month == cd.get(Calendar.MONTH)) {
if (day > cd.get(Calendar.DAY_OF_MONTH)) {
System.out.print("Invalid date!");
System.exit(0);
}
}
}
}
} catch (NumberFormatException ne) {
System.out.print(ne.getMessage() + " is not a legal entry!");
System.out.print("Please enter number.");
System.exit(0);
}
Calendar bd = new GregorianCalendar(year, month, day);
ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
if (cd.before(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))) {
ageYears--;
ageMonths = (12 - (bd.get(Calendar.MONTH) + 1))
+ (bd.get(Calendar.MONTH));
if (day > cd.get(Calendar.DAY_OF_MONTH)) {
ageDays = day - cd.get(Calendar.DAY_OF_MONTH);
} else if (day < cd.get(Calendar.DAY_OF_MONTH)) {
ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
} else {
ageDays = 0;
}
} else if (cd.after(new GregorianCalendar(cd.get(Calendar.YEAR), month,
day))) {
ageMonths = (cd.get(Calendar.MONTH) - (bd.get(Calendar.MONTH)));
if (day > cd.get(Calendar.DAY_OF_MONTH))
ageDays = day - cd.get(Calendar.DAY_OF_MONTH) - day;
else if (day < cd.get(Calendar.DAY_OF_MONTH)) {
ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
} else
ageDays = 0;
} else {
ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
ageMonths = 0;
ageDays = 0;
}
System.out.print("Age of the person : " + ageYears + " year, "
+ ageMonths + " months and " + ageDays + " days.");
}
}

Calculate Age:
import java.text.*;
import java.util.*;
public class CalculateAge{
static String errorMessage;
public static boolean isValidDate(String date){
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
Date testDate = null;
try
{
testDate = sdf.parse(date);
}
catch (ParseException e)
{
errorMessage = "the date you provided is in an invalid date" + " format.";
return false;
}
if (!sdf.format(testDate).equals(date))
{
errorMessage = "The date that you provided is invalid.";
return false;
}
return true;
}
public static void main(String[]args){
System.out.println("Enter your Date of Birth in format MM-dd-yyyy");
Scanner input=new Scanner(System.in);
String dateOfBirth=input.nextLine() ;
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("MM-dd-yyyy");
String currentDate=sdf.format(d);
try {
if(isValidDate(dateOfBirth)){
Calendar cal1 = new GregorianCalendar();
Calendar cal2 = new GregorianCalendar();
int age = 0;
int factor = 0;
Date date1 = new SimpleDateFormat("MM-dd-yyyy").parse(dateOfBirth);
Date date2 = new SimpleDateFormat("MM-dd-yyyy").parse(currentDate);
cal1.setTime(date1);
cal2.setTime(date2);
if(cal2.get(Calendar.DAY_OF_YEAR) < cal1.get(Calendar.DAY_OF_YEAR)) {
factor = -1;
}
age = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR) + factor;
System.out.println("Your age is: "+age+" years");
}
else{
System.out.println(errorMessage);
}
}catch (ParseException e) {
System.out.println(e);
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.