Wednesday, July 3, 2024
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)
 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments