Create a simple java program that does the following: 1. The program asks the user to enter his name. 2. Reads the value of the name and store it in an appropriate variable. 3. Then it asks the user to enter his birth year. 4. Reads the value of the y
Sure! Below is a simple Java program that fulfills your requirements. The program prompts the user for their name and birth year, calculates their age, and displays the information back to them.
```java
import java.util.Scanner;
public class UserInformation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt user for their name
System.out.print("Enter your name: ");
String name = scanner.nextLine();
// Prompt user for their birth year
System.out.print("Enter your birth year: ");
int birthYear = scanner.nextInt();
// Calculate age
int currentYear = 2024;
int age = currentYear - birthYear;
// Print user information
System.out.println("--- User Information ---");
System.out.println("Name: " + name);
System.out.println("Birth Year: " + birthYear);
System.out.println("Age: " + age);
// Close the scanner
scanner.close();
}
}
```
### How to Run the Program:
1. Make sure you have Java installed on your computer.
2. Open a text editor (like Notepad, VSCode, or any IDE).
3. Copy the above code into the editor.
4. Save the file as `UserInformation.java`.
5. Open the command line or terminal.
6. Navigate to the directory where you saved the file.
7. Compile the program using the command:
```
javac UserInformation.java
```
8. Run the program using the command:
```
java UserInformation
```
### Example Output:
```
Enter your name: Arwa
Enter your birth year: 1995
--- User Information ---
Name: Arwa
Birth Year: 1995
Age: 29
```
Just replace "Arwa" and "1995" with your desired name and birth year after running the program.


