Monday, July 13, 2009

Scanners in Java 5

Java.util.Scanner class in java 5 is mainly used for tokenizing data and finding stuff. Although, Scanners in java 5 doesn’t provide location information or search and replace functionality, it can be used to source data by applying regular expressions to find out how many instances of an expression exists in source data.

Tokenizing

Tokenizing is the process of taking big pieces of source data, breaking them into pieces and storing the pieces into variables. Besides scanners in java 5, tokenizing functionality is provided by String (with the help of split() method).
For the purpose of Tokenizing, source data comprises of tokens and delimiters. Tokens are the actual pieces of data, and delimiters are the expressions that separate tokens from each other.

For Example:

Source: “123,23,india,delhi”
If we say that our delimiter is a comma, then our four tokens would be

123
23
india
delhi

Tokenizing with Scanner

Let’s look at it using an example. Scanner’s default delimiter is white space.

Scanner methods:
  1. hasNextXxx() - tests the value of the next token but do not actually get the token. For example, hasNextDouble() checks if the next token is of double data type.
  2. nextXxx() - returns the next token
  3. useDelimeter() - allows you to set the delimiter to be any valid regular expression.

Code Snippet:

import java.util.Scanner;
class ScanProg {
public static void main(String [] args) {
boolean b1, b2;
int i;
String s, hitsCntr = " ";
Scanner s1 = new Scanner(args[0]);
Scanner s2 = new Scanner(args[0]);
while(b1 = s1.hasNext()) {
s = s1.next();
hitsCntr += "s";
}
while(b1 = s2.hasNext()) {
if (s2.hasNextInt()) {
i = s2.nextInt();hitsCntr += "i";
}
else if (s2.hasNextBoolean()) {
b2 = s2.nextBoolean();hitsCntr += "b1";
}
else {
s2.next();
hitsCntr += "s2";
}
}
System.out.println("hitsCntr " + hitsCntr);
}
}

If this program is invoked with
java ScanNext "2 false 46 tty"
it produces

hitsCntr ssssibis2

No comments:

Post a Comment