0
0
Javaprogramming~15 mins

Reading integer input in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
Reading integer input
📖 Scenario: You are creating a simple Java program that asks the user to enter their age. This is a common task when you want to get information from someone using a program.
🎯 Goal: Build a Java program that reads an integer input from the user and then prints it back to confirm what was entered.
📋 What You'll Learn
Use the Scanner class to read input
Read an integer value from the user
Store the input in a variable called age
Print the value of age with a message
💡 Why This Matters
🌍 Real World
Reading integer input is common in programs that ask users for numbers, like age, quantity, or scores.
💼 Career
Many programming jobs require reading and validating user input, especially in console applications and backend services.
Progress0 / 4 steps
1
Set up the Scanner object
Write a line to create a Scanner object called scanner that reads from System.in.
Java
Need a hint?

Use Scanner scanner = new Scanner(System.in); to read input from the keyboard.

2
Read an integer input
Write a line to read an integer from scanner and store it in an int variable called age.
Java
Need a hint?

Use int age = scanner.nextInt(); to read an integer input.

3
Print the entered age
Write a line to print the message "You entered: " followed by the value of age using System.out.println.
Java
Need a hint?

Use System.out.println("You entered: " + age); to show the input.

4
Close the Scanner and run
Add a line to close the scanner object using scanner.close(). Then run the program and enter 25 as input. The output should be You entered: 25.
Java
Need a hint?

Use scanner.close(); to close the scanner and free resources.