Scanner Class tutorial with examples in java

This blog post covers tutorials on the Scanner class with examples in the Java language.

Java.util.Scanner Class in Java

The Scanner class is a fundamental class provided by the Java language as part of the JDK installation, residing in the java.util package.

The java.util.Scanner class is a way to take input from the user’s keyboard in Java. It reads input in the form of Java primitive data types like Integer and String. Internally, the Scanner class in Java uses a regular expression to parse the input, employing the default space delimiter.

The Scanner class was introduced in Java 1.5 onwards.

How to Declare the Scanner Class in Java?

The Scanner class in the JDK has the following declaration

public final class Scanner
  extends Object
  implements Iterator, Closeable

The Scanner class implements the Iterator and Closeable interfaces.

The Iterator class is used to iterate over scanned primitive types, and Closeable is employed to release 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);
    }
}

Output: Scanner Console Example= Scanner Usage example if Scanner Usage example is provided as input.

Scanner Examples in Java

The java.util.Scanner class can be used to read data from various sources.

How to Read Input Data from the Console Using the Scanner Class

The Scanner API in Java provides various constructors for reading from different sources such as File, InputStream, java.nio.file.Path, and java.nio.channels.ReadableByteChannel.

System.in provides the capability to read from the console.

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

The Scanner constructor can take 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 the Scanner Class

The NoSuchElementException is a RuntimeException that occurs when there is no element to scan while using the Scanner class in Java.

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();
}

The nextInt(), nextLong() methods also throw NoSuchElementException. To fix this, the code needs to use corresponding hasNextInt(), hasNextLong() methods.

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

The following example expects 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 the read integer number when an integer is provided, but an InputMismatchException is thrown when a float number is provided.

Ensure correct input data entry and handle the appropriate method to read data.

Advantages of java.util.Scanner Class

  • Simplifies text reading capabilities using the Scanner class.
  • Scanner has a default regular expression for primitive data types and strings, eliminating the need for regular expression compilation and improving the performance of the java.util.Scanner class.

Scanner class disadvantages

  • Not suitable for multithread applications due to a lack of thread safety.
  • Synchronization needs to be handled; the Java compiler waits until data is entered by the user during execution when using the Scanner class.