The StringTokenizer class:
Particularly when processing input data, there is often a need to read a string and split it up into individual tokens, each one representing a separate, meaningful item, such as a number or a word. In many programming languages such a process is complex to program, but the java.util library class StringTokenizer makes this very easy. An example of a StringTokenizer object is shown below:
StringTokenizer tokensIn =
new StringTokenizer(stringIn," \n\t\r");
This sets up a tokenizer that allows extraction from the string stringIn of all the substrings that are terminated by any of the delimiter characters. The delimiter characters are within the string that is the second argument of the constructor - in this case, space, newline, tab and carriage return. These characters are collectively known as white space. Thus it picks up any substring that is terminated by white space and, of course, the final substring.
The StringTokenizer class includes two important methods, which carry out traversal of the string and checking that the final substring has been extracted.
- String nextToken() extracts the next string delimited by any of the delimiter characters and prepares the tokenizer for extraction of the following token. If there are no tokens left when this method is called, a NoSuchElementException will be thrown (but see hasMoreTokens()).
- boolean hasMoreTokens() returns true if there are more tokens to be processed in the string that is being traversed.
An example of this class being used is shown below. It shows code that finds, within the string eMailAddress, the component substrings that are terminated by a full stop, the @ symbol or the end of the string. It then prints these out.
StringTokenizer em = new StringTokenizer(eMailAddress, "@.");
while (em.hasMoreTokens())
{
System.out.println(em.nextToken());
}
Since Java 5, a new approach to breaking strings into tokens is to use the String.split() method, which requires you to understand what are known as 'regular expressions'.
The StringTokenizer class is now considered a legacy class and is therefore no longer the recommended way to split strings, but has been retained because you are likely to encounter it in recent code and because you can use it without studying regular expressions.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your The StringTokenizer class homework and assignments? Live The StringTokenizer class experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer The StringTokenizer class homework help, java assignment help and The StringTokenizer class projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours