Scanner in Java – Complete Guide for Beginners and Developers
Java is one of the most widely used programming languages for building desktop applications, web applications, enterprise systems, and Android apps. One of the most important tasks in programming is taking input from users. In Java, one of the most popular and beginner-friendly ways to read user input is through the Scanner class.
In this blog, we will explore what Scanner is, why it is used, how it works, its methods, real-world examples, common mistakes, and best practices.
What is Scanner in Java?
The Scanner class is part of the java.util package and is used to take input from different sources such as:
- Keyboard input
- Files
- Strings
- Input streams
It was introduced in Java 5 to simplify input handling. Before Scanner, developers used classes like BufferedReader, which were more complex for beginners.
Why Scanner is Important
Scanner is widely used because:
- It is easy to use
- It supports multiple data types
- It reduces coding complexity
- It is beginner-friendly
- It works well for console-based applications
For example, if you want to build a simple calculator or student record system, Scanner makes input handling simple and readable.
How to Use Scanner in Java
To use Scanner, you must first import it.
import java.util.Scanner;
Then create a Scanner object:
Scanner sc = new Scanner(System.in);
Here:
System.inmeans keyboard inputscis the Scanner object
Basic Example of Scanner
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("Hello " + name);
sc.close();
}
}
This program takes user input and prints it.
Common Scanner Methods
1. nextLine()
Reads full line input (including spaces).
String text = sc.nextLine();
2. next()
Reads single word input.
String word = sc.next();
3. nextInt()
Reads integer input.
int num = sc.nextInt();
4. nextDouble()
Reads decimal numbers.
double value = sc.nextDouble();
5. nextBoolean()
Reads boolean input (true/false).
boolean flag = sc.nextBoolean();
Taking Multiple Inputs Example
Scanner sc = new Scanner(System.in);
System.out.print("Enter age: ");
int age = sc.nextInt();
System.out.print("Enter salary: ");
double salary = sc.nextDouble();
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
Scanner with Loops
Scanner is often used with loops to take repeated input.
Scanner sc = new Scanner(System.in);
for(int i = 1; i <= 3; i++) {
System.out.print("Enter number: ");
int num = sc.nextInt();
System.out.println("You entered: " + num);
}
Scanner with Conditional Logic
Scanner sc = new Scanner(System.in);
System.out.print("Enter marks: ");
int marks = sc.nextInt();
if(marks >= 50) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
Scanner Reading from File
Scanner can also read from files.
File file = new File("data.txt");
Scanner sc = new Scanner(file);
while(sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
Common Mistakes When Using Scanner
1. Not Closing Scanner
Always close Scanner to prevent resource leaks.
sc.close();
2. Mixing nextLine() with nextInt()
Example problem:
int num = sc.nextInt();
String name = sc.nextLine();
Fix:
int num = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();
3. Input Mismatch Exception
If user enters wrong data type, program crashes.
Solution: Use validation or try-catch.
Best Practices for Scanner
✔ Always close Scanner
✔ Validate user input
✔ Handle exceptions
✔ Use correct method for data type
✔ Avoid creating multiple Scanner objects for System.in
Scanner vs BufferedReader
| Feature | Scanner | BufferedReader |
|---|---|---|
| Ease of Use | Easy | Moderate |
| Performance | Slightly slower | Faster |
| Parsing Support | Built-in | Manual |
| Beginner Friendly | Yes | Less |
Real-World Use Cases
Console Applications
Used in small tools and practice programs.
Data Entry Programs
Used in student or employee management systems.
Learning Programming
Most beginner Java programs use Scanner.
Competitive Programming
Used sometimes for input reading.
Limitations of Scanner
- Slower than BufferedReader for large input
- Can throw runtime exceptions
- Not ideal for high-performance systems
When to Use Scanner
Use Scanner when:
- Building small applications
- Learning Java
- Writing console programs
- Input size is small to medium
Avoid Scanner when:
- Processing huge data
- Building performance-critical systems
Future and Relevance
Even though modern Java frameworks use advanced input methods, Scanner remains highly relevant for learning, prototyping, and small tools.
Conclusion
The Scanner class is one of the simplest and most useful tools for taking input in Java. It allows developers to read different types of data easily and build interactive programs quickly. Understanding Scanner is essential for anyone starting Java programming.
By learning Scanner methods, avoiding common mistakes, and following best practices, you can write efficient and reliable Java programs. While it may not be ideal for high-performance systems, it remains a powerful tool for learning and everyday development tasks.