Java Utility Library

Java Scanner - remove() Method



The java.util.Scanner.remove() method is used to remove operation which are not supported by the implementation of Iterator.

Syntax

public void remove()

Parameters

No parameter is required.

Return Value

void type.

Exception

Throws UnsupportedOperationException - if the method is invoked.

Example:

In the example below, the java.util.Scanner.remove() method is attempted to call which raises exception.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {

    //String to scan
    String MyString = "Hello World 10 + 20 = 30.0";

    //creating a Scanner
    Scanner MyScan = new Scanner(MyString);

    //print the nextLine token
    System.out.println(MyScan.nextLine());

    //attempt to call remove raises exception
    MyScan.remove();

    //close the scanner
    MyScan.close();
  }
}

The output of the above code will be:

Hello World 10 + 20 = 30.0

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.base/java.util.Scanner.remove(Scanner.java:1490)
    at MyClass.main(MyClass.java:16)

❮ Java.util - Scanner