Home »
Java programming language
Java Scanner useDelimiter() Method with Example
Scanner Class useDelimiter() method: Here, we are going to learn about the useDelimiter() method of Scanner Class with its syntax and example.
Submitted by Preeti Jain, on March 24, 2020
Scanner Class useDelimiter() method
Syntax:
public Scanner skip(Pattern patt);
public Scanner skip(String patt);
- useDelimiter() method is available in java.util package.
- useDelimiter(Pattern patt) method is used to put the delimiter pattern to the given pattern (patt) of this Scanner.
- useDelimiter(String patt) method is used to put the delimiter pattern to the pattern formed from the given string (patt) of this Scanner.
- These methods don’t throw an exception at the time of the set delimiter pattern.
- These are non-static methods and it is accessible with the class object only and if we try to access these methods with the class name then we will get an error.
Parameter(s):
-
In the first case, useDelimiter(Pattern patt),
- Pattern patt – represents the delimiter pattern.
-
In the second case, useDelimiter(String patt),
- String patt – represents a string denoting the delimiter pattern.
Return value:
In both the cases, the return type of the method is Scanner, it retrieves this Scanner object.
Example 1:
// Java program to demonstrate the example
// of useDelimiter() method of Scanner
import java.util.*;
import java.util.regex.*;
public class UseDelimiter {
public static void main(String[] args) {
String str = "Java Programming! 3 * 8= 24";
// Instantiates Scanner
Scanner sc = new Scanner(str);
// By using useDelimiter(Pattern) method
// is to use delimiter set by the given
// pattern
sc.useDelimiter(Pattern.compile("IH--"));
System.out.println("sc.useDelimiter(Pattern): " + sc.delimiter());
// Scanner closed
sc.close();
}
}
Output
sc.useDelimiter(Pattern): IH--
Example 2:
import java.util.*;
import java.util.regex.*;
public class UseDelimiter {
public static void main(String[] args) {
String str = "Java Programming! 3 * 8= 24";
// Instantiates Scanner
Scanner sc = new Scanner(str);
// By using useDelimiter(String) method
// is to use delimiter set by the given
// string
sc.useDelimiter("IH--");
System.out.println("sc.useDelimiter(String): " + sc.delimiter());
// Scanner closed
sc.close();
}
}
Output
sc.useDelimiter(String): IH--