Learn how to make Java programs interactive using the Scanner class. Step-by-step guide to reading user input, using variables, and building a simple calculator for numbers and decimals. Perfect for beginners!
The Scanner class in Java is a built-in utility class that allows your program to read input from various sources, most commonly from the keyboard (user input). It’s part of the java.util package, so you need to import it before using it:
import java.util.Scanner;
Scanner:Purpose: The Scanner class reads data typed by the user, such as text, numbers, or boolean values, and stores it in variables for your program to use.
Creating a Scanner Object: Before reading input, you create a Scanner object and connect it to a source. For keyboard input:
Scanner input = new Scanner(System.in);
input → the name of the Scanner objectSystem.in → tells Scanner to listen to the standard input (keyboard)Common Methods:
| Method | Reads | Example Input |
|---|---|---|
nextLine() |
Whole line of text (String) |
Hello World |
next() |
Single word (String) |
Hello |
nextInt() |
Integer (int) |
25 |
nextDouble() |
Decimal number (double) |
9.5 |
nextBoolean() |
True/False (boolean) |
true |
To make your program interactive, we use a built-in Java tool called the Scanner. This allows the program to pause and wait for the user to type something on their keyboard.
This program shows how to make your Java program interactive. Instead of just printing fixed messages, it can ask the user for input and respond dynamically.
Save the code in a file named:
InputExample.java
import java.util.Scanner; // Step 1: Import the Scanner class
public class InputExample {
public static void main(String[] args) {
// Step 2: Create a Scanner object to read input
Scanner reader = new Scanner(System.in);
System.out.print("Enter your name: ");
// Step 3: Read the input typed by the user
String name = reader.nextLine();
System.out.println("Nice to meet you, " + name + "!");
// Step 4: Close the Scanner to free resources
reader.close();
}
}
Think of Scanner as a “telephone line” connecting the keyboard to your code. The program waits for the user to type and then responds.
import java.util.Scanner;
“I want to use the Scanner class from the utility library.”
Scanner reader = new Scanner(System.in);
reader → the name of your Scanner objectSystem.in → tells Scanner to listen to the keyboardString name = reader.nextLine();
nextLine() waits for the user to type a full line of text and press Enter.name.System.out.println("Nice to meet you, " + name + "!");
+).reader.close();
⚠️ Common Issue: Mixing nextInt() and nextLine()
If you use nextInt() (or nextDouble()) and then immediately call nextLine(), Java may skip the input.
✅ Fix: Add an extra nextLine() after nextInt() to clear the leftover newline.
int age = reader.nextInt();
reader.nextLine(); // clears the input buffer
String name = reader.nextLine();
💡 For absolute beginners, it’s easiest to use nextLine() for all input at first.
Enter your name: Ali
Nice to meet you, Ali!
❌ Forgetting import statement ❌ Wrong file name ❌ Not closing scanner ❌ Using wrong input method
Modify the program to ask:
Then print:
Hello Ali, you are 15 years old!
Create a program that asks:
Then prints a personalized message.
Create a file named Calculator.java and paste this in:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("--- Java Addition Tool ---");
// 1. Get the first number
System.out.print("Enter first number: ");
double num1 = input.nextDouble();
// 2. Get the second number
System.out.print("Enter second number: ");
double num2 = input.nextDouble();
// 3. Perform the calculation
double sum = num1 + num2;
// 4. Output the result
System.out.println("The total is: " + sum);
input.close();
}
}
double TypeWe used double instead of int. Why? Because int only handles whole numbers (1, 2, 3). If a user types 5.5, an int would crash the program. A double can handle both.
Java uses standard symbols for math:
+-*/Just like in math class, Java follows PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction). If you want to do a complex calculation, use parentheses:
double result = (num1 + num2) * 10;
If you try to type a letter when the program asks for a nextDouble(), the program will crash with an InputMismatchException. Java is very strict—if it expects a number, you must give it a number!