{

Scanner Class tutorial with examples in java


In this blog post, I will covert the scanner class tutorials with examples of Java language.

Scanner Class in java

Scanner class is one of the basic default classes provided by Java language as part of JDK installation. Scanner class is located in the java.util package.

Java.util.Scanner class is one way to take the input from the user keyboard in java.

It read the input in the form of java primitive data types like Integer and String. Internally Scanner class in Java uses a regular expression to parse the input using the default space delimiter. Scanner class introduced in Java 1.5 onwards

Java.util.Scanner class declaration

Scannar.java class in JDK as the following declaration

public final class Scanner  
extends Object  
implements Iterator, Closeable  

Scanner class implements Iterator and closeable interfaces. Iterator class is used to iterate scanned primitive types, Closeable is used to release the resources once the scanner reads the source.

  
import java.util.Scanner;  
  
public class ScannerExample {  
      public static void main(String args[]) {  
            Scanner readInput = new Scanner(System.in);  
            String line = readInput.nextLine();  
            System.out.println("Scanner Console Example= " + line);  
      }  
}  
 And the output  is Scanner Console Example= Scanner Usage example if Scanner Usage example is provided as input. 

Scanner examples in java

We can use java.util.scanner class is used to read data from various source

How to read the input data from the console using Scanner class

Scanner API in Java provides various constructors for reading various sources like File, InputStream, java.nio.file.Path, java.nio.channels.ReadableByteChannel System. in provides the reading capabilities to read from the console. This option provides capabilities to read data from the console-based on input data.

How to read the input data from File stream using Scanner class in java

  
The scanner constructor has a file reader reference to read input data from files.  
Scanner fileScannerRead = new Scanner (new FileReader(new File("FILE_PATH")));  

How to Fix NoSuchElementException using Scanner class

NoSuchElementException exception is RuntimeException occurred when there is no element to scan while using Scanner class in java NoSuchElementException occurs when code has Scanner.next() method To fix this, the code needs to check whether the next element is available or not.

Scanner readInput = new Scanner(System.in);  
if (readInput.hasNext()) {  
                  Object obj = readInput.next();  
            }  
  

nextInt(),nextLong() methods also throws NoSuchElementException . To fix, Code has to use corresponding hasNextInt(),hasNextLong() methods.

Fixing Exception in thread “main” java.util.InputMismatchException

The following example expects the input as a number from the console.

  
Public static void main(String args[]) {  
            Scanner readInput = new Scanner(System.in);  
        System.out.print("Enter input number : ");  
        int inputNo = readInput.nextInt();  
}  

The above program, prints read the integer number when the integer number is provided. InputMismatchException exception throws when float number is provided as input. Please make sure that you enter the correct input data and handle the correct method to read data.

java.util.Scanner class advantages

  • Simplifies the text reading capabilities using the scanner class
  • Scanner has default regular expression for primitive data types and String. So Regular expression compile is not required and hence improved the performance of java.util.Scanner class

Disadvantages

  • Scanner is not good for multithread applications for thread safety.
  • Need to handle synchronization Java Compiler encountered Scanner class during execution, compiler wait until data is entered by the user.
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.