Scanner Class in JavaLast Updated : 29 Jan 2026 Java provides various ways to read input from the user, and the Scanner class is one of the simplest and most commonly used approaches. In this chapter, we will learn what the Scanner class is, why it is used, its declaration, constructors, important methods, and examples to understand how it works for reading input and parsing data. What is Scanner Class in Java?The Scanner class is used to read input data from different sources such as keyboard input, files, strings, or streams. It breaks the input into tokens using a delimiter, which is whitespace by default, and allows us to read data in different primitive types like int, long, double, float, byte, boolean, and String. The Scanner class belongs to the java.util package. It extends the Object class and implements the Iterator<String> and Closeable interfaces. Scanner is widely used because it provides easy-to-use methods like nextInt(), nextLine(), nextDouble(), and nextBoolean() to parse input efficiently. Scanner Class DeclarationThe Scanner class is defined in the java.util package. How to Get a Scanner Object?Reading Input from KeyboardTo read input from the user, we pass System.in to the Scanner constructor. Reading Input from a StringTo parse data from a string, we pass the string to the Scanner constructor. Constructors of Scanner ClassThe Scanner class provides multiple constructors to read input from different sources. 1. Scanner(InputStream source)This constructor creates a Scanner that reads input from an input stream such as System.in. Syntax: Here is the syntax: 2. Scanner(InputStream source, String charsetName)This constructor creates a Scanner that reads input from an input stream using the specified character encoding. Syntax: Here is the syntax: 3. Scanner(String source)This constructor creates a Scanner that reads input from the specified string. Syntax: Here is the syntax: 4. Scanner(File source)This constructor creates a Scanner that reads input from a file. Syntax: Here is the syntax: 5. Scanner(File source, String charsetName)This constructor creates a Scanner that reads input from a file using the specified character encoding. Syntax: Here is the syntax: 6. Scanner(Readable source)This constructor creates a Scanner that reads input from any object implementing the Readable interface. Syntax: Here is the syntax: 7. Scanner(ReadableByteChannel source)This constructor creates a Scanner that reads input from a readable byte channel. Syntax: Here is the syntax: 8. Scanner(ReadableByteChannel source, String charsetName)This constructor creates a Scanner that reads input from a readable byte channel using the specified character encoding. Syntax: Here is the syntax: 9. Scanner(Path source)This constructor creates a Scanner that reads input from a file path. Syntax: Here is the syntax: 10. Scanner(Path source, String charsetName)This constructor creates a Scanner that reads input from a file path using the specified character encoding. Syntax: Here is the syntax: Methods of Scanner ClassThe Scanner class provides many useful methods to read and parse input.
Examples of Java Scanner ClassPractice the following examples to understand different uses of the Scanner class. Example 1: Reading a String from UserThe following example demonstrates how to read a string input from the user using the nextLine() method. Output: Enter your name: Jack Name is: Jack Example 2: Reading Multiple Data TypesThe following example demonstrates how to read different data types such as String, int, and double using Scanner. Output: Enter your name: Andrew Enter your age: 23 Enter your salary: 25000 Name: Andrew Age: 23 Salary: 25000.0 Example 3: Using Scanner with DelimiterThe following example demonstrates how to change the delimiter and tokenize a string using the Scanner class. Output: --- Tokenized Output --- Hello This is TpointTech My name is Jack. Delimiter used: / Next TopicFile Handling in Java |
We request you to subscribe our newsletter for upcoming updates.