- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
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}+
To Continue Learning Please Login
Login with Google