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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the useDelimiter()
// 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();
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the useDelimter()
// 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();
    }
}

chevron_right


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)



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.