0% found this document useful (0 votes)
43 views2 pages

Java Age Calculator Program

The document describes an age calculator program written in Java. The program uses a Gregorian calendar to get the current year and prompts the user to enter their birth year. It then calculates the user's age by subtracting their birth year from the current year. The program displays the user's age and allows them to continue calculating more ages or exit the program.

Uploaded by

D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Java Age Calculator Program

The document describes an age calculator program written in Java. The program uses a Gregorian calendar to get the current year and prompts the user to enter their birth year. It then calculates the user's age by subtracting their birth year from the current year. The program displays the user's age and allows them to continue calculating more ages or exit the program.

Uploaded by

D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Age calculator

import java.util.*;
import javax.swing.*;

class age_calculator {

Boolean reply = true;

public void display()


{

GregorianCalendar now = new GregorianCalendar();

while(reply) {

int now_year=0,birth_year=0,years_old=0;
String output;

output = "In what year you are born?";

try
{
birth_year = Integer.parseInt
(JOptionPane.showInputDialog(null,output));
}

catch (final NumberFormatException e)


{

output = "Please type in a numerical value." +


"\n"
+ "Try Again ";

JOptionPane.showMessageDialog(null,output,
"Error
Report",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}

now_year = now.get(GregorianCalendar.YEAR);
years_old = now_year - birth_year;

output = "This is the year you become "


+ years_old + " years old.";
JOptionPane.showMessageDialog(null,output);
output = "Do you want to continue";
int n = JOptionPane.showConfirmDialog(null,output,
"Yes/No", JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.NO_OPTION)
{
reply = false;
System.out.print("\t Thank you for using this
Program");
System.out.print("\n\n");
System.exit(0);
}
}
}
}

class age {

public static void main(String[] args)


{

age_calculator person = new age_calculator();

person.display();
}
}

You might also like