Answer to Question #308667 in Java | JSP | JSF for Dunga

Question #308667

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.

1
Expert's answer
2022-03-10T10:19:11-0500


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();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS