Write a program in Java that ask the user to enter their name, year of birth, and salary. The program must display thier name, age, and salary.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.*;
class App {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = keyboard.nextLine();
System.out.print("Enter your year of birth (dd/MM/yyyy): ");
String yearBirth = keyboard.nextLine();
System.out.print("Enter your salary: ");
double salary = keyboard.nextDouble();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date birthDate;
try {
birthDate = sdf.parse(yearBirth);
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(birthDate.getYear(), birthDate.getMonth(), birthDate.getDay());
Period period = Period.between(birthday, today);
System.out.println("Name: " + name);
System.out.println("Age: " + (period.getYears() - 1900));
System.out.println("Salary: " + salary);
} catch (ParseException e) {
e.printStackTrace();
}
keyboard.close();
}
}
Comments
Leave a comment