Java Scanner Class Explained
If you're learning Java, one of the first things you'll need is taking input from the user. That's where the Scanner class comes in.
In this post, you'll learn:
- What is Scanner?
- Why do we use it?
- How to use it?
- Common Scanner methods
- Difference between
next()andnextLine() - Common mistakes
- Interview questions
What is Scanner?
Scanner is a predefined class in Java used to read input from different sources such as:
- Keyboard (
System.in) - Files
- Strings
It belongs to the java.util package, so we must import it before using it.
import java.util.Scanner;
Why Do We Use Scanner?
Normally, a Java program prints fixed output.
System.out.println("Hello Java");
But if you want the user to enter data while the program is running, you need the Scanner class.
For example:
Enter your name:
The program waits for the user's input and processes it.
Creating a Scanner Object
Scanner sc = new Scanner(System.in);
Explanation:
-
Scanner→ Class name -
sc→ Object name -
new→ Creates a new object -
System.in→ Reads input from the keyboard
Example: Reading 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
Enter your name:
Arul
Welcome Arul
Common Scanner Methods
| Method | Description | Example |
|---|---|---|
next() |
Reads one word | Java |
nextLine() |
Reads an entire line | Java Full Stack |
nextInt() |
Reads an integer | 25 |
nextDouble() |
Reads a decimal number | 95.5 |
nextFloat() |
Reads a float | 3.14 |
nextBoolean() |
Reads true or false
|
true |
nextLong() |
Reads a long value | 9876543210 |
Reading an Integer
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Age is " + age);
sc.close();
Output
Enter your age:
22
Age is 22
Reading Multiple Inputs
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println(name);
System.out.println(age);
sc.close();
Difference Between next() and nextLine()
next()
Reads only one word.
Scanner sc = new Scanner(System.in);
String name = sc.next();
Input
Java Programming
Output
Java
It stops reading when it encounters a space.
nextLine()
Reads the entire line.
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
Input
Java Programming
Output
Java Programming
Common Mistake
Scanner sc = new Scanner(System.in);
System.out.print("Age: ");
int age = sc.nextInt();
System.out.print("Name: ");
String name = sc.nextLine();
System.out.println(name);
Input
22
Arul
The program prints an empty string for name.
Why?
nextInt() reads only the number and leaves the Enter key (\n) in the input buffer. The following nextLine() immediately reads that leftover newline.
Solution
int age = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();
Why Do We Close Scanner?
sc.close();
Closing the Scanner releases the resources associated with it. It is considered a good programming practice to close it after use.
Interview Questions
1. What is Scanner?
Scanner is a predefined class in the java.util package used to read input from the keyboard, files, strings, and other input sources.
2. Why do we use Scanner?
We use Scanner to take input from the user while the program is running.
3. Which package contains Scanner?
java.util
4. How do you create a Scanner object?
Scanner sc = new Scanner(System.in);
5. What is the difference between next() and nextLine()?
next() |
nextLine() |
|---|---|
| Reads one word | Reads the entire line |
| Stops at whitespace | Reads until Enter is pressed |
Conclusion
The Scanner class is one of the first Java classes every beginner should learn. It allows you to read user input easily and supports many data types such as int, double, String, and boolean.
Understanding methods like nextInt(), next(), and nextLine() will help you avoid common mistakes and write better Java programs.