Page 315 - ComputerScience_Class_11
P. 315
The output of the preceding program is as follows:
BlueJ: Terminal Window - Java
Options
2022
20.22
10.5 STRINGTOKENIZER CLASS
The StringTokenizer class splits the given string into tokens (a sequence of characters except for delimiters) or words.
By default, it takes space, \t, \n \r and \f as separators. The syntax is:
StringTokenizer object = new StringTokenizer(String variable);
Let us see the below example:
StringTokenizer st=new StringTokenizer("ISC class 11");
This creates a StringTokenizer object “st” which contains “ISC class 11” and it is separated into three tokens or words.
If we want other characters as delimiters, then we have to use the following syntax:
StringTokenizer object = new StringTokenizer(String variable, "delimiter");
Let us see the below example:
StringTokenizer st=new StringTokenizer("ISC-class 11-String Chapter", "-" );
This creates a StringTokenizer object “st” which contains “ISC-class 11-String Chapter” and it is separated into three
tokens or words.(1. ISC, 2. class 11 and 3. String Chapter)
The StringTokenizer class has the following methods:
• nextToken(): It returns the next token/word from the StringTokenizer object.
• hasMoreTokens(): It returns true if there is a token left in the StringTokenizer object, else it returns false.
• countTokens(): It returns the total number of tokens in the object.
• hasMoreElements(): It is same as hasMoreTokens() and returns true if there is a token left in the StringTokenizer
object, else it returns false.
10.6 JAVA LIBRARY CLASS
Some methods are provided by Java to make coding easy. These built-in methods are available in the library class which
can be imported into the code dynamically at runtime. This library further contains packages.
Packages are a group of similar type of classes, interfaces and sub-packages, which contains some built-in classes
containing methods, that are required during coding. Packages can be categorised into two, namely, built-in packages
and user-defined packages.
There are many built-in packages that are commonly used such as java, lang, awt, javax, swing, net, io, util, sql, etc.
To include a package in a program, the syntax is
import java.<packagename>.<classname>;
For examples:
import java.util.*;
import java.io.*;
The ”lang” package is the default package in Java and when we use it, we do not need to use the ‘import’ command
as it is automatically executed.
Strings 313

