🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The delimiter() method in Java, part of the java.util.Scanner class, is used to retrieve the delimiter pattern that the Scanner object is currently using to tokenize the input.
Table of Contents
- Introduction
delimiter()Method Syntax- Understanding
delimiter() - Examples
- Basic Usage
- Custom Delimiter
- Real-World Use Case
- Conclusion
Introduction
The delimiter() method returns the Pattern that the Scanner is currently using to tokenize the input. By default, the Scanner uses whitespace as the delimiter.
delimiter() Method Syntax
The syntax for the delimiter() method is as follows:
public Pattern delimiter()
Parameters:
- This method does not take any parameters.
Returns:
- The
PatterntheScanneris currently using to tokenize the input.
Understanding delimiter()
The delimiter() method allows you to get the current delimiter pattern of the Scanner. The delimiter pattern is used to separate the tokens in the input stream.
Examples
Basic Usage
To demonstrate the basic usage of delimiter(), we will create a Scanner object and retrieve its default delimiter pattern.
Example
import java.util.Scanner;
import java.util.regex.Pattern;
public class ScannerDelimiterExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Pattern delimiter = scanner.delimiter();
System.out.println("Default delimiter: " + delimiter.pattern());
scanner.close();
}
}
Output:
Default delimiter: \p{javaWhitespace}+
Custom Delimiter
This example shows how to set a custom delimiter for the Scanner and then retrieve it using the delimiter() method.
Example
import java.util.Scanner;
import java.util.regex.Pattern;
public class CustomDelimiterExample {
public static void main(String[] args) {
String input = "apple,banana,cherry";
Scanner scanner = new Scanner(input);
scanner.useDelimiter(",");
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
Pattern delimiter = scanner.delimiter();
System.out.println("Custom delimiter: " + delimiter.pattern());
scanner.close();
}
}
Output:
apple
banana
cherry
Custom delimiter: ,
Real-World Use Case
Parsing CSV Files
In real-world applications, the Scanner class can be used to parse CSV files by setting a custom delimiter.
Example
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CSVParsingExample {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(new File("data.csv"))) {
scanner.useDelimiter(",");
while (scanner.hasNext()) {
String data = scanner.next();
System.out.print(data + " | ");
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
}
}
In this example, the Scanner is configured to use a comma (,) as the delimiter, making it suitable for parsing CSV files.
Output (Assuming data.csv contains "apple,banana,cherry"):
apple | banana | cherry |
Conclusion
The Scanner.delimiter() method is useful for retrieving the current delimiter pattern used by the Scanner to tokenize input. By understanding and using this method, you can efficiently parse and process different types of input data, such as space-separated or comma-separated values. Whether you are working with default or custom delimiters, the delimiter() method provides a reliable way to handle tokenization in your Java applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment