The Scanner class is one of the simplest and most commonly used ways to take input from the keyboard in Java. If console input is the door through which information enters a program, Scanner is one of the tools Java gives us to open that door.
What Is the Scanner Class?
Scanner is a class from the java.util package that allows a Java program to read input from different sources. For beginner console programs, we commonly use it with System.in to read keyboard input.
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);
}
}
There are two important parts to notice here: Scanner and System.in. Scanner provides the methods used to read the input, while System.in represents the standard input stream, normally connected to the keyboard when the program runs in a console.
Importing Scanner
Scanner belongs to the java.util package, so we need to import it before using the class.
import java.util.Scanner;
The import statement tells Java that the Scanner class we want to use is available inside the java.util package.
If you use Scanner in a normal Java program, remember the import statement: import java.util.Scanner;
Creating a Scanner Object
After importing Scanner, we create an object so that we can use its input methods.
Scanner sc = new Scanner(System.in);
This single line is worth understanding carefully. Scanner is the class type, sc is the variable that refers to the Scanner object, and new Scanner(System.in) creates a Scanner that reads from standard input.
Reading a String
Scanner provides next() and nextLine() for reading text.
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("Name: " + name);
}
}
Here, nextLine() reads the complete line entered by the user. This means it can read text containing spaces, such as a full name.
next() vs nextLine()
This difference is important for beginners. next() reads one token, while nextLine() reads the remaining content of the current line.
System.out.print("Enter your name: ");
String name = sc.next();
System.out.println("Name: " + name);
If the user enters Bibhu Sahoo, next() reads only Bibhu.
If we use nextLine(), the complete input Bibhu Sahoo can be read.
| Method | Input Example | Value Read |
|---|---|---|
| next() | Bibhu Sahoo | Bibhu |
| nextLine() | Bibhu Sahoo | Bibhu Sahoo |
Reading an Integer
To read an integer value, Scanner provides the nextInt() method.
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Age: " + age);
If the user enters 25, Scanner converts the entered input into an integer value and stores it in the age variable.
Reading Decimal Values
Scanner also provides methods for reading decimal numbers.
System.out.print("Enter your percentage: ");
double percentage = sc.nextDouble();
System.out.println("Percentage: " + percentage);
For example, if the user enters 82.5, the value is stored as a double.
Common Scanner Methods
| Scanner Method | Data Type | Typical Use |
|---|---|---|
| nextInt() | int | Whole numbers |
| nextLong() | long | Large whole numbers |
| nextFloat() | float | Decimal numbers |
| nextDouble() | double | More precise decimal numbers |
| next() | String | One word or token |
| nextLine() | String | Complete line of text |
| nextBoolean() | boolean | true or false |
Reading Multiple Inputs
A Scanner object can be used repeatedly to collect several values from the user.
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.print("Enter your age: ");
int age = sc.nextInt();
System.out.print("Enter your marks: ");
double marks = sc.nextDouble();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks: " + marks);
}
}
Notice that the same sc object is used for all three inputs. We do not need to create a new Scanner object every time we want to read another value.
A Beginner Mistake to Watch For
A common problem appears when nextInt() is followed immediately by nextLine().
int age = sc.nextInt(); String name = sc.nextLine();
The numeric input method does not consume the line break at the end of the entered line. As a result, the following nextLine() may consume that remaining line break instead of waiting for the user's text.
A common beginner-friendly solution is to consume the remaining line first.
int age = sc.nextInt(); sc.nextLine(); String name = sc.nextLine();
When mixing numeric Scanner methods such as nextInt() with nextLine(), pay attention to the leftover line break. This small detail causes many beginner input bugs.
Closing the Scanner
When the Scanner is no longer needed, it can be closed.
sc.close();
For simple standalone console programs, closing the Scanner at the end is commonly seen. However, beginners should understand that closing a Scanner connected to System.in also closes the underlying input stream. Therefore, another part of the program cannot continue reading from that same standard input stream afterward.
Interview Insight
If an interviewer asks why Scanner is commonly used for console input, a good answer is: Scanner provides convenient methods for reading different types of data from an input source such as System.in.
A stronger answer also distinguishes next() from nextLine() and shows awareness of the common newline issue when mixing them with numeric input methods.
Quick Revision
| Concept | Remember |
|---|---|
| Scanner | Class used to read input. |
| java.util.Scanner | Package and class used by Scanner. |
| System.in | Standard input source, normally the keyboard. |
| next() | Reads one token. |
| nextLine() | Reads a complete line. |
| nextInt() | Reads an integer. |
| nextDouble() | Reads a double value. |
| close() | Closes the Scanner and its underlying input stream. |
The Scanner class gives Java beginners a practical way to make programs interactive. Once you understand how to create a Scanner and choose the correct input method, you can build programs that respond to real user data instead of relying only on fixed values.
