Java
Scanner
StringTokenizer
String.Split
Programming

Scanner vs. StringTokenizer vs. String.Split

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In Java, parsing strings is a common task that can significantly impact the performance and readability of your code. Three popular classes to accomplish string parsing include Scanner, StringTokenizer, and String.split(). Each has its own strengths and weaknesses, which makes it essential to understand their differences and use-cases. Let's explore these options in detail, comparing their functionality, performance, and typical applications.

Scanner

Scanner is a versatile and powerful utility class, primarily used for parsing input from various input streams like System.in, files, and strings. It provides methods to read and parse primitive data types and strings using regular expressions or delimiters.

Key Features

  • Flexibility: Offers extensive methods for parsing different data types (e.g., nextInt(), nextDouble()).
  • Regular Expressions: Uses regular expressions for delimiter splitting, which allows complex parsing operations.
  • Buffered Input: Internally uses buffering, which optimizes performance when reading from streams or files.

Example

java
1import java.util.Scanner;
2
3public class ScannerExample {
4    public static void main(String[] args) {
5        String input = "23 apples 45 oranges";
6        Scanner scanner = new Scanner(input);
7        while (scanner.hasNext()) {
8            if (scanner.hasNextInt()) {
9                System.out.println("Integer: " + scanner.nextInt());
10            } else {
11                System.out.println("String: " + scanner.next());
12            }
13        }
14        scanner.close();
15    }
16}

Considerations

  • Performance: Generally slower compared to StringTokenizer or String.split() for simple parsing tasks due to the overhead of regex processing.
  • Resource-Intensive: Not suitable for very large datasets or high-performance applications due to memory consumption.

StringTokenizer

StringTokenizer is an older utility used for splitting strings into tokens based on specified delimiters. It is part of the java.util package and provides a basic mechanism for tokenization.

Key Features

  • Simplicity: Designed for straightforward tokenization, often faster for simple tasks.
  • Ease of Use: Provides methods like nextToken() for sequential token iteration.
  • Delimiter-Based: Primarily uses single-character delimiters, limiting its flexibility.

Example

java
1import java.util.StringTokenizer;
2
3public class StringTokenizerExample {
4    public static void main(String[] args) {
5        String input = "banana,apple,orange";
6        StringTokenizer st = new StringTokenizer(input, ",");
7        while (st.hasMoreTokens()) {
8            System.out.println(st.nextToken());
9        }
10    }
11}

Considerations

  • Obsolescence: Considered a legacy class; not recommended for new applications.
  • Limited Flexibility: Cannot handle complex scenarios like multi-character delimiters without substantial workarounds.

String.split()

The String.split() method uses regular expressions to divide a string into an array of substrings. Introduced in JDK 1.4, it is widely used due to its simplicity and power.

Key Features

  • Regular Expressions: Utilizes regex for defining delimiters, offering flexibility.
  • Simplicity: Easy to use for straightforward splitting tasks, returns an array of strings.
  • Performance: Efficient for moderate-sized inputs with simple splitting rules.

Example

java
1public class StringSplitExample {
2    public static void main(String[] args) {
3        String input = "dog:cat:bird";
4        String[] animals = input.split(":");
5        for (String animal : animals) {
6            System.out.println(animal);
7        }
8    }
9}

Considerations

  • Complexity Limit: For very complex tokenization, managing regex expressions can become cumbersome.
  • Resource Consumption: The split operation creates an array, which could be a memory concern for extremely large strings.

Summary Table

Feature/AspectScannerStringTokenizerString.split()
FlexibilityHigh (supports multiple data types and regex)Low (single-character delimiters only)Medium (regex support)
Ease of UseModerate (more setup required)High (straightforward to use)High (simple API)
PerformanceModerate (regex overhead)Fast (minimal processing)Fast (depends on regex complexity)
Use-CasesComplex parsing tasks with mixed data typesSimple tokenization with static delimitersModerate complexity, regex-based splitting
Legacy StatusModern APILegacyModern API

Additional Considerations

Locale-Sensitive Parsing

When dealing with Scanner, it is crucial to note that it is locale-sensitive, which means that it can interpret numbers differently based on the set locale (e.g., decimal points versus commas). This may lead to inconsistent behavior unless correctly configured.

Handling Edge Cases

  • Empty Strings: When using String.split(), an empty input string returns an array with a single empty element, which can be counterintuitive.
  • Trimming and Whitespace: Consider additional trimming or stripping of whitespace when parsing with StringTokenizer and String.split(), as they do not handle these inherently.

Choosing the Right Tool

When selecting a utility for parsing tasks, consider the complexity of your input strings, the need for regular expressions, and performance constraints. For simple tasks, StringTokenizer or String.split() might be suitable, whereas Scanner shines in scenarios requiring multi-type parsing.

In conclusion, understanding the distinctions among Scanner, StringTokenizer, and String.split() enables Java developers to choose the most effective tool for their string parsing needs, balancing performance, ease of use, and flexibility.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.