Introduction
Taking input from users is one of the most important concepts in Java programming. Whether you are creating a simple calculator, login system, student management application, or banking software, your program often needs information from the user. Java provides several ways to accept input, but the Scanner class is the most popular and beginner-friendly approach.
The Scanner class is part of the java.util package and allows developers to read different types of input such as integers, floating-point numbers, strings, characters, and boolean values from the keyboard.
For example, instead of displaying fixed values, a Java program can ask the user to enter their name, age, or salary and then process the entered data accordingly.
What is Java Input using Scanner?
The Scanner class is a predefined Java class that is used to read input from various sources such as:
- Keyboard
- Files
- Strings
- Input streams
For beginners, Scanner is mainly used to accept keyboard input from users.
The Scanner class belongs to the following package:
import java.util.Scanner;
Since Scanner belongs to the java.util package, you must import it before using it.
Why use Scanner?
Scanner provides methods for reading different data types such as:
- Integer
- Float
- Double
- Long
- Boolean
- String
- Character (using a workaround)
Without Scanner, Java programs would be much harder to interact with users.
Features of Scanner
- Easy to use
- Reads multiple data types
- Supports token-based input
- Reads complete lines
- Beginner-friendly
- Widely used in Java applications
Syntax
Step 1: Import Scanner
import java.util.Scanner;
Step 2: Create Scanner Object
Scanner sc = new Scanner(System.in);
Here,
- Scanner → Class name
- sc → Object name
- System.in → Keyboard input stream
Step 3: Read Input
int age = sc.nextInt();
Step 4: Close Scanner
sc.close();
Closing the Scanner object helps free system resources.
Scanner Methods
| Method | Description |
|---|---|
| next() | Reads a single word |
| nextLine() | Reads an entire line |
| nextInt() | Reads an integer |
| nextDouble() | Reads a double value |
| nextFloat() | Reads a float value |
| nextBoolean() | Reads true/false |
| nextLong() | Reads a long integer |
| nextShort() | Reads short value |
| nextByte() | Reads byte value |
Example 1: Read an Integer
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Age = " + age);
sc.close();
}
}
Output
Age = 25
Example 2: Read a String
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Welcome " + name);
sc.close();
}
}
Output
Welcome Rahul Sharma
Example 3: Read Multiple Inputs
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = sc.nextLine();
System.out.print("Enter Age: ");
int age = sc.nextInt();
System.out.print("Enter Salary: ");
double salary = sc.nextDouble();
System.out.println("Name : " + name);
System.out.println("Age : " + age);
System.out.println("Salary : " + salary);
sc.close();
}
}
Output
Enter Age: 28
Enter Salary: 45000
Example 4: Reading a Character
Scanner does not provide a direct method for reading a character.
Instead, use:
char gender = sc.next().charAt(0);
Complete Example
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Gender (M/F): ");
char gender = sc.next().charAt(0);
System.out.println("Gender = " + gender);
sc.close();
}
}
Example 5: Reading Boolean Value
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Are you a student? ");
boolean student = sc.nextBoolean();
System.out.println(student);
sc.close();
}
}
Output
true
Example 6: Reading Multiple Numbers
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
int sum = a + b;
System.out.println("Sum = " + sum);
sc.close();
}
}
Output
Enter second number: 20
Sum = 35
Difference between next() and nextLine()
| next() | nextLine() |
|---|---|
| Reads one word | Reads complete line |
| Stops at space | Includes spaces |
| Used for single words | Used for full names or sentences |
Example
String name = sc.next();
Input:
Rahul Kumar
Output:
Real-life Example
Suppose you are creating a Student Registration System.
The application asks the user to enter:
- Student Name
- Roll Number
- Age
- Percentage
Program
import java.util.Scanner;
public class Student {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Student Name: ");
String name = sc.nextLine();
System.out.print("Roll Number: ");
int roll = sc.nextInt();
System.out.print("Age: ");
int age = sc.nextInt();
System.out.print("Percentage: ");
double percentage = sc.nextDouble();
System.out.println("\nStudent Details");
System.out.println("--------------------");
System.out.println("Name: " + name);
System.out.println("Roll: " + roll);
System.out.println("Age: " + age);
System.out.println("Percentage: " + percentage);
sc.close();
}
}
Output
Roll Number: 101
Age: 20
Percentage: 88.5
Student Details
--------------------
Name: Priya Sharma
Roll: 101
Age: 20
Percentage: 88.5
This is a common scenario in school management systems, admission portals, and educational applications.
Common Mistakes
1. Forgetting to Import Scanner
Incorrect:
Scanner sc = new Scanner(System.in);
Correct:
import java.util.Scanner;
2. Forgetting to Create Scanner Object
Incorrect:
int age = nextInt();
Correct:
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
3. Mixing nextInt() and nextLine()
Incorrect:
int age = sc.nextInt();
String name = sc.nextLine();
The nextInt() method leaves the newline character (\n) in the input buffer, so nextLine() may read an empty string.
Correct:
int age = sc.nextInt();
sc.nextLine(); // Consume leftover newline
String name = sc.nextLine();
4. Entering Wrong Data Type
int age = sc.nextInt();
Input:
Twenty
Result:
InputMismatchException
Always enter the correct data type or validate user input before processing.
5. Not Closing Scanner
Incorrect:
Scanner sc = new Scanner(System.in);
Correct:
sc.close();
Closing the Scanner object releases the underlying input resource.
Best Practices
- Always import java.util.Scanner.
- Create only one Scanner object for System.in.
- Close the Scanner when it is no longer needed.
- Use nextLine() for full names and sentences.
- Handle invalid input using exception handling for robust applications.
- Be careful when switching between numeric input methods and nextLine().
Conclusion
The Scanner class is one of the easiest and most widely used ways to take user input in Java. It enables programs to interact with users by reading various data types such as integers, floating-point numbers, strings, booleans, and more. By importing java.util.Scanner, creating a Scanner object with System.in, and using the appropriate input methods, you can quickly build interactive applications.