Home »
Java programming language
Java Scanner remove() Method with Example
Scanner Class remove() method: Here, we are going to learn about the remove() method of Scanner Class with its syntax and example.
Submitted by Preeti Jain, on February 18, 2020
Scanner Class remove() method
- remove() method is available in java.util package.
- remove() method is used to result an exception during the call remove() method.
- remove() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- remove() method may throw an exception at the time of throwing an exception.
UnsupportedOperationException: This exception may throw when this method is unsupported.
Syntax:
public void remove();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of the method is void, it returns nothing.
Example:
// Java program to demonstrate the example
// of void remove() method of Scanner
import java.util.*;
public class RemoveOfScanner {
public static void main(String[] args) {
String str = "Hi, true \n IncludeHelp! 8 + 2.0f = 10.0f";
// Instantiate Scanner with the
//given str
Scanner sc = new Scanner(str);
//Display Scanner
System.out.println("sc.nextLine(): " + sc.nextLine());
// By using remove() method will
// result an exception
sc.remove();
// close the scanner
sc.close();
}
}
Output
sc.nextLine(): Hi, true
Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.Scanner.remove(Scanner.java:1490)
at RemoveOfScanner.main(RemoveOfScanner.java:19)