Java Scanner delimiter() Method



Description

The java Scanner delimiter() method returns the Pattern this Scanner is currently using to match delimiters.

Declaration

Following is the declaration for java.util.Scanner.delimiter() method

public Pattern delimiter()

Parameters

NA

Return Value

This method returns this scanner's delimiting pattern.

Exception

NA

Getting Delimiter used by Scanner on a String Example

The following example shows the usage of Java Scanner delimiter() method to print the delimiter used by the scanner object. We've created a scanner object using a given string. Then we printed the string using scanner.nextLine() method and then delimiter used by the scanner is printed using delimiter() method.

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // print the next line of the string
      System.out.println(scanner.nextLine());

      // print the delimiter this scanner is using
      System.out.println(scanner.delimiter());

      // close the scanner
      scanner.close();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Hello World! 3 + 3.0 = 6
\p{javaWhitespace}+

Getting Custom Delimiter used by Scanner on a String Example

The following example shows the usage of Java Scanner delimiter() method to print the delimiter used by the scanner object. We've created a scanner object using a given string and a given delimiter. Then we printed the string using scanner.nextLine() method and then delimiter used by the scanner is printed using delimiter() method.

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      String input = "1 abc 2 abc";
      Scanner scanner = new Scanner(input).useDelimiter("\\s*abc\\s*");
      System.out.println(scanner.nextInt());
      System.out.println(scanner.nextInt());
      // print the delimiter this scanner is using
      System.out.println(scanner.delimiter());

      // close the scanner
      scanner.close();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

1
2
\s*abc\s*

Getting Delimiter used by Scanner on User Input Example

The following example shows the usage of Java Scanner delimiter() method to print the delimiter used by the scanner object. We've created a scanner object using System.in class. Then we printed the string using scanner.nextLine() method and then delimiter used by the scanner is printed using delimiter() method.

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(System.in);

      // print the next line of the string
      System.out.println(scanner.nextLine());

      // print the delimiter this scanner is using
      System.out.println(scanner.delimiter());

      // close the scanner
      scanner.close();
   }
}

Output

Let us compile and run the above program, this will produce the following result − (where we've entered Hello World and pressed enter key.)

Hello World
Hello World
\p{javaWhitespace}+
java_util_scanner.htm
Advertisements