In Java 1.5, a new class known as Scanner class was introduced to simplify the task of getting input from the user. The Scanner class is in java.util package which allows the user to read the data dynamically from the keyboard. It can be used to read a File on the disk. The Java Scanner class extends Object class is present in java.lang package and implements Iterator and Closeable interfaces.
The Java Scanner class that breaks down its input into tokens using any regular expression delimiter that is whitespace by default and it’s useful for parsing files also. Scanner class provides many methods to read and parse various primitive byte, short, int, float, double or even an object of String class. Java Scanner class commonly used to parse a string of text and primitive types using a regular expression. Before using a Scanner class, the program must import the package import java.util.*; OR Java.util.Scanner;.
For example, java.util.Scanner specifies that Scanner is a class in the package util and util is a package in the package java. A Scanner class to create and obtain input from command window is as follows:
Scanner input = new Scanner(System.in);
As per the above syntax
• You can declare scanner type object say ‘input.’
• To create a Scanner object, the new keyword used.
•The Scanner class constructor, which takes an InputStream object (i.e., System.in) as a parameter. The System.in specifies the standard console keyboard input.
We’ll be covering the following topics in this tutorial:
How to construct a Java Scanner Class?
We can create an instance of the Scanner class using one of the following constructors:
• Scanner(File source): It constructs a new Scanner that scans the specified file using the system’s default charset.
• Scanner(File source, String charsetName): It constructs a new Scanner that scans the specified file using the specified character set.
• Scanner(InputStream source): It constructs a new Scanner from a byte input stream using the system’s default charset.
• Scanner(InputStream source, String charsetName): It constructs a new Scanner from a byte input stream using the specified charset.
• Scanner(Readable source): It constructs a new Scanner from a character stream.
• Scanner(String source): It constructs a new Scanner from a String.
• Scanner(ReadableByteChannel source): constructs a Scanner that produces values scanned from the specified channel.
• Scanner(ReadableByteChannel source, String charsetName): constructs a Scanner that produces values scanned from the specified channel.
• Scanner(Path source): constructs a Scanner that produces values scanned from the specified file.
• Scanner(Path source, String charsetName): constructs a Scanner that produces values scanned from the specified file.
The various methods of the scanner class are as follows:
The nextShort, nextFloat, and nextLong methods in Scanner work similarly. Each of these methods (except nextLine) reads a set of consecutive characters until whitespace (such as a blank or a tab) encountered. The nextLine method reads all of the data that a user typed on a line before pressing the Enter key on a keyboard.
We can create a scanner that reads data from the console by using a System.in argument to the Scanner constructor:
Scanner scanner = new Scanner(System.in);
The value returned by a method can copy into a variable whose data type matches that of the returned value. For instance, the integer value that is returned by the nextInt method copy into another variable called number1 using an assignment statement:
int number1 = scanner.nextlnt();
If the user enters a value that is different from the type expected (such as a floating-point number), a run-time error result.
The following statement reads a string from the console by using the next method and stores it in the variable named string1:
String string1 = scanner.next();
The variables number1 and string1 can be used as needed in the rest of the program.
We now make a small program that inputs two integers in Java and display result. In this program, we use the methods available in Scanner class to read data typed at the keyboard and place it into variables that we specify.
import java.util.*;
class InputDemo {
public static void main (String args []) {
int a,b;
double pi;
String name;
Scanner s = new Scanner(System.in);
System.out.printIn(“Enter thevalue of a & b”);
a = s.nextInt();
b = s.nextInt();
System.out.printIn(“Enter the value of pi”);
pi = s.nextDouble();
System.out.printIn(“Enter the your name”);
name = s.next();
System.out.printIn(“The value of a = “+a);
System.out.println(“The value of b = “+b);
System.out.print1n(“The value of pi = “+pi);
System.out.println(“Name = “+name );
}
}
Advantage of using Scanner class in java
• Java Scanner class can do all that a BufferedReader class does with the same efficiency.
• Java Scanner is not thread safe, but BufferedReader is thread safe.
• Java Scanner class has various methods to manipulate input stream data.
• Java Scanner class tokenizes the underlying stream using a delimiter that is white space by default.
• The string manipulation in scanner class is entirely more comfortable as each word can be obtained as a token and handled separately.
• Java Scanner class can parse the underlying stream for string and primitive types like int, short, float, double, and long using regular expression.
• Java BufferedReader class can read only String but Scanner can read both String and primitive types.
• Java Scanner class use (1KB) little buffer size as comapre to BufferdReader class has a significantly large buffer (8KB), that means if you were reading Lengthy data from a file, then use BufferedReader but for short you use Scanner class.
• Java BufferedReader class is synchronized while Scanner class is not. Which means, you cannot share Scanner class between multiple threads, but you can share the BufferedReader class object.
• Java Scanner class extends Object class and implements Iterator and Closeable interfaces.
The disadvantage you may face with Scanner are
• Java Scanner class is not synchronised.
• Java Scanner is not thread-safe.
• Java Scanner has little buffer memory (1KB).
• Java Scanner class can parse the underlying stream of data bit slower.
• Java Scanner class comes from JDK 1.5 higher.