Thursday, April 18, 2013

Parsing delimited text using java Scanner


We all have come across the task of processing delimited text numerous times.

Since 1.5 version java provides the java.util.Scanner class which contains useful methods to process delimited text.
Consider a comma delimited text containing employee details firstname, lastname, department, city, state, zip.

For example lets parse the comma separated text
john, doe, d1, true, city1, state1, 1234

We can process this text using java Scanner as


























Here is our Employee class,















Note that useDelimiter takes a regular expression as an argument.
The regular expression "\\s*,\\s*" will consider any comma with preceeding or succeeding whitespaces as a delimiter.

Lets run this with the below main method:









The following output is printed:

Employee name is john doe

Using String split to process delimited text:


Processing delimited text is one of the most widely encountered task by a programmer.
java.lang.String class provides a very useful split() method to parse delimited strings. Lets take a look at it.
Consider a comma delimited text containing the employee details firstname, lastname, department, city, state, zip.

Lets parse the comma separated text
john, doe, d1, true, city1, state1, 1234

For the split method we pass the delimiter as ",\\s*" regular expression. This will delimit on any comma followed by zero or more white spaces.
We can process this text using java String split as:
















The Employee class is:


Lets run it with a main method:

The output of running this code is:
Employee name after split is john doe

Hope this post usefule. Please leave your comments.

Check out my other blogs:
http://www.techusergit.blogspot.com/
http://www.techusercamel.blogspot.com