String Tokenizer in Java
The string tokenizer class allows an application to break a string into tokens. The tokenization method is much simpler than the one used by the StreamTokenizer class. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.
The set of delimiters (the characters that separate tokens) may be specified either at creation time or on a per-token basis.
An instance of StringTokenizer behaves in one of two ways, depending on whether it was created with the returnDelims flag having the value true or false:
If the flag is false, delimiter characters serve to separate tokens. A token is a maximal sequence of consecutive characters that are not delimiters.
If the flag is true, delimiter characters are themselves considered to be tokens. A token is thus either one delimiter character or a maximal sequence of consecutive characters that are not delimiters.
A StringTokenizer object internally maintains a current position within the string to be tokenized. Some operations advance this current position past the characters processed.
A token is returned by taking a substring of the string that was used to create the StringTokenizer object.
The following is one example of the use of the tokenizer.
public class TokenizerTest { public static void main(String args[]){ StringTokenizer st = new StringTokenizer("this is a test"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } } } output: this is a test
import java.util.StringTokenizer;
Constructors of StringTokenizer class
There are 3 constructors defined in the StringTokenizer class
1). StringTokenizer(String str) creates StringTokenizer with a specified string.
2). StringTokenizer(String str, String delim) creates StringTokenizer with specified string and delimiter.
3). StringTokenizer(String str, String delim, boolean returnValue) creates StringTokenizer with specified string, delimiter and returnValue. If the return value is true, delimiter characters are considered to be tokens. If it is false, delimiter characters serve to separate tokens.
The 6 useful methods of StringTokenizer class
String nextToken() String nextToken(String delim) boolean hasMoreElements() Object nextElement() int countTokens()
boolean hasMoreTokens()
import java.util.*;
public class TokenizerTest1 {
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("this,is,a,test");
// printing next token
System.out.println("Next token is : " + st.nextToken(","));
}
}
Output:
Next token is : this
StringTokenizer Using Java 8 Approach
As Tokenizer implements the Enumeration interface, we can use it with Java‘s Collections interface.
Reading From CSV File we have file abc.csv
2| Anna | anna@gmail.com
1| Bala | bala@gmail.com
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class ReadFile {
public static void main(String[] args) {
BufferedReader br = null;
try {
String line;
br = new BufferedReader(new FileReader("c:/abc.csv"));
while ((line = br.readLine()) != null) {
System.out.println(line);
StringTokenizer stringTokenizer = new StringTokenizer(line, "|");
while (stringTokenizer.hasMoreElements()) {
Integer id = Integer.parseInt(stringTokenizer.nextElement().toString());
Double price = Double.parseDouble(stringTokenizer.nextElement().toString());
String username = stringTokenizer.nextElement().toString();
StringBuilder sb = new StringBuilder();
sb.append("\nId : " + id);
sb.append("\nUsername : " + username);
sb.append("\nEmail : " + email);
sb.append("\n");
System.out.println(sb.toString());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
1| Bala | bala@gmail.com Id : 1 Username : Bala Email : bala@gmail.com 2| Anna | anna@gmail.com Id : 1 Username : Anna Email : anna@gmail.com
output:
In this article, we discussed StringTokenizer class in Java with example.
0 Comments
Post a Comment