Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Scanner: Text Parsing Made Easy

DZone's Guide to

Java Scanner: Text Parsing Made Easy

Java's Scanner class, and the occasional bit of RegEx, makes parsing text trivial. You can put this to good use in a number of ways, which we'll dive into here.

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

“The only way of discovering the limits of the possible is to venture a little way past them into the impossible.”
― Arthur C. Clarke

Java provides a Scanner class that can be used as a text parser. It accepts a regular expression as a delimiter and returns tokens separated by the delimiter. Let us look at some usage scenarios of the Scanner class.

Image title


Count Words in a File

The default delimiter used by the Scanner is whitespace. It returns text tokens separated by whitespace. Let us use this fact to count the words in a file. The following code prints the value as well as its index from the file. Note that the Scanner implements the AutoCloseable interface so we can use it in try-with-resources block.

try (Scanner scanner = new Scanner(new File(filename));) {
    int nword = 0;
    while (scanner.hasNext()) {
    String sent = scanner.next();
    nword++;
    System.out.printf("%3d) %s%n", nword, sent);
    }
}


Read Text by Paragraph

Specifying an empty-line regex as the delimiter allows you to read text by paragraphs. The regular expression pattern specifies the multi-line flag, so use ^ and $ match at the beginning and end of each line, rather than the whole input.

try (Scanner scanner = new Scanner(new File(filename));) {
    scanner.useDelimiter("(?m:^$)");
    int ntoken = 0;
    while (scanner.hasNext()) {
    String token = scanner.next();
    ntoken++;
    System.out.printf("%3d) %s%n", ntoken, token);
    }
}


Scanner Trick: Read a Whole File

To read the whole file in a single String, use the following. The delimiter here is the regular expression for the beginning of the file.

try (Scanner scanner = new Scanner(new File(filename));) {
    scanner.useDelimiter("\\A");
    String all = scanner.next();
}


Summary

The Java class Scanner is used for text parsing. By setting the delimiter appropriately, various parsing tasks can be accomplished.

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
scanner ,java ,regular expression ,tutorial

Published at DZone with permission of Jay Sridhar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}