Scan-Do Attitude: Parsing Complex Syntax with the Scanner Class
In the previous section, we read a data file using the Scanner class in Java 5. It was simple enough, because each line had the same structure. What if we wanted to read a data file that has a structure that is different for each line of text? A Matcher wouldn't work, because it can use only a single regex. The Scanner can be used for these kinds of tasks, because you can use regular expressions on the input text to look ahead for patterns in the upcoming text. Because you can read the input on a token-by-token basis, you can write custom parsers for any kind of text. Let's illustrate ...
Get Wicked Cool Java now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.