- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Scanner Class in Java
Introduction
The Java Scanner class is a simple text scanner that can parse primitive types (int, double, long, etc.) and strings using regular expressions. The Scanner class plays an important role in Java by providing user-based input.
The Scanner class is a part of java.util package, and a scanner can read text from any object that implements the Readable interface. When we close a Scanner, it will close its input source if the source implements the Closeable interface.
Characteristics of the Scanner class
The following are some of the important characteristics of the Scanner class in Java −
- A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
- A scanning operation may block while waiting for input.
- A Scanner is not safe for multithreaded use without external synchronization.
Class declaration
F ollowing is the declaration for the Scanner class −
public final class Scanner extends Object implements Iterator<String>
How to use the Scanner Class?
The scanner class can be used by following the steps below −
Step 1
The first and important step is to import the java.util.Scanner at the top of our code, as without the Scanner class, we cannot use its methods. We can either use the java.uti.Scanner or java.util.* package to work with the Scanner class.
import java.util.Scanner //OR import java.util.*
Step 2
The second step is to create the object of the Scanner class. Here, the "sc" is the Scanner class object, and it allows the user to read from the System. To create an object of the Scanner class, call the Scanner() constructor.
Scanner sc = new Scanner(System.in);
Step 3
The third step is to create a variable, and on that variable, use the methods from the Scanner class object to take user input.
int number = sc.nextInt();
After using the Scanner class, use the close() method to close the Scanner operations.
Example
Below is an example of how to input a number using the Scanner class in Java −
//Step 1: import the Scanner class
import java.util.Scanner;
public class DemoScanner {
public static void main(String[] args) {
// Step 2: Create Scanner object
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
// Step 3: Read the number from console
int number = sc.nextInt();
System.out.println("The entered number is: " + number);
// Close the scanner
sc.close();
}
}
Output
This will produce the following result −
Enter a number: 10 The entered number is: 10
Uses of Scanner Class
The following are some of the use cases of the Scanner class in Java −
- The Scanner class is used for reading and analyzing data stored in text files(logs, configuration files).
- Helps developers in building console-based applications, such as a Student management system.
- It can also be used to parse tokens, which helps in Data Parsing from CSV files.
Class constructors
The following are the Scanner class constructors present in Java −
| Sr.No. | Constructor & Description |
|---|---|
| 1 |
Scanner(File source) This constructs a new Scanner that produces values scanned from the specified file. |
| 2 |
Scanner(File source, String charsetName) This constructs a new Scanner that produces values scanned from the specified file. |
| 3 |
Scanner(File source, Charset charset) This constructs a new Scanner that produces values scanned from the specified file. |
| 4 |
Scanner(InputStream source) This constructs a new Scanner that produces values scanned from the specified input stream. |
| 5 |
Scanner(InputStream source, String charsetName) This constructs a new Scanner that produces values scanned from the specified input stream. |
| 6 |
Scanner(InputStream source, Charset charset) This constructs a new Scanner that produces values scanned from the specified input stream. |
| 7 |
Scanner(Readable source) This constructs a new Scanner that produces values scanned from the specified source. |
| 8 |
Scanner(String source) This constructs a new Scanner that produces values scanned from the specified source. |
| 9 |
Scanner(ReadableByteChannel source) This constructs a new Scanner that produces values scanned from the specified channel. |
| 10 |
Scanner(ReadableByteChannel source, String charsetName) This constructs a new Scanner that produces values scanned from the specified channel. |
| 11 |