Sunday, September 22, 2024
Google search engine
HomeLanguagesJavaScanner useDelimiter() method in Java with Examples

Scanner useDelimiter() method in Java with Examples

useDelimiter(Pattern pattern)

The useDelimiter(Pattern pattern) method of java.util.Scanner class sets this scanner’s delimiting pattern to the specified pattern.

Syntax: 

public Scanner useDelimiter(Pattern pattern)

Parameter: The function accepts a mandatory parameter pattern which specifies a delimiting pattern.

Return Value: The function returns this scanner.

Below programs illustrate the above function:

Program 1:  

Java




// Java program to illustrate the useDelimiter(Pattern Pattern)
// method of Scanner class in Java
 
import java.util.*;
import java.util.regex.Pattern;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        String s = "Geeksforgeeks has Scanner Class Methods";
 
        // create a new scanner
        // with the specified String Object
        Scanner scanner = new Scanner(s);
 
        // print a line of the scanner
        System.out.println("Scanner String: \n"
                           + scanner.nextLine());
 
        // display the old delimiter
        System.out.println("Old delimiter: "
                           + scanner.delimiter());
 
        // change the delimiter of this scanner
        scanner.useDelimiter(Pattern.compile(".ll."));
 
        // display the new delimiter
        System.out.println("New delimiter: "
                           + scanner.delimiter());
 
        // close the scanner
        scanner.close();
    }
}


Output: 

Scanner String: 
Geeksforgeeks has Scanner Class Methods
Old delimiter: \p{javaWhitespace}+
New delimiter: .ll.



 

useDelimiter(String pattern)

The useDelimiter(String pattern) method of java.util.Scanner class Sets this scanner’s delimiting pattern to a pattern constructed from the specified String.

Syntax:  

public Scanner useDelimiter(String pattern)

Parameter: The function accepts a mandatory parameter string pattern which specifies a delimiting pattern.

Return Value: The function returns this scanner.

Below programs illustrate the above function:

Program 1:  

Java




// Java program to illustrate the useDelimter(String Pattern)
// method of Scanner class in Java
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        String s = "Geeksforgeeks has Scanner Class Methods";
 
        // create a new scanner
        // with the specified String Object
        Scanner scanner = new Scanner(s);
 
        // print a line of the scanner
        System.out.println("Scanner String: \n"
                           + scanner.nextLine());
 
        // print the old delimiter
        System.out.println("Old Delimiter: "
                           + scanner.delimiter());
 
        // change the delimiter
        scanner.useDelimiter("Wor");
 
        // print the new delimiter
        System.out.println("New Delimiter: "
                           + scanner.delimiter());
 
        // close the scanner
        scanner.close();
    }
}


Output: 

Scanner String: 
Geeksforgeeks has Scanner Class Methods
Old Delimiter: \p{javaWhitespace}+
New Delimiter: Wor



 

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#useDelimiter(java.lang.String)
 

RELATED ARTICLES

Most Popular

Recent Comments