Page 355 - Cs_withBlue_J_C11_Flipbook
P. 355
Some of the methods of the PrintStream class are given in the following table.
Method Description
print() It has 9 overloaded methods for printing data of
different data types namely int, float, long, double,
boolean, character, character array, object and String.
After printing, the cursor remains in the same line.
println() It also has 9 overloaded methods for printing data of
different types as mentioned above. After printing,
the cursor goes to the starting of the next line.
printf() It is used to output a formatted string to the console
using various format specifiers. It includes two
parameters: formatted string and argument.
The above methods cannot be used directly with the object of the PrintStream class. Instead, we have to either use
System.out.print() or System.out.println(). Let us examine the three parts separately:
• System: It is a final class (a class which cannot be extended or inherited defined in java.lang package.
• out: It is a class variable of the PrintStream type, which is a public and static member of the System class. Since it is
a static member, it is accessed using the class name; hence System.out is referenced.
• print() or println(): These are public methods of PrintStream class. Since out is of PrintStream type, it is used to call
the methods print() and println().
12.2 THE STRINGTOKENIZER CLASS
The StringTokenizer is a class defined under java.util package. It is used to split a String into tokens. As you know that
tokens are separated by delimiters. Whitespace, newline, and carriage return are the default delimiters, though other
characters can be included. The methods of the StringTokenizer class do not distinguish among identifiers, numbers
and quoted texts. Let us explain with the following example:
Say the String is, “Raj scored 92 in Maths, 88 in Physics.”
If space, comma (,) and fullstop (.) are defined as the delimiters, then the tokens are:
Raj scored 92 in Maths 88 in Physics
The StringTokenizer class handles tokens more efficiently than the String class, where a text is analysed character by
character, to check for the delimiters. In the absence of the delimiters, it concatenates the characters to form a token.
To use StringTokenizer class, we have to import the java.util package by using the following statement.
import java.util.*;
OR
import java.util.StringTokenizer;
The overloaded constructors of the StringTokenizer class are given in the following table.
Constructor Purpose
StringTokenizer(String str) It creates a StringTokenizer object with a specified
string considering only the default delimiters.
StringTokenizer(String str, String delimiter) It creates a StringTokenizer object with a specified
string and a String of delimiters list.
353
Basic Input/Output and Data File Handling 353

