Page 270 - Cs_withBlue_J_C11_Flipbook
P. 270
double d=Double.valueOf(str2);
// converts "20.23" to 20.23.
System.out.println(str1);
System.out.println(str2);
Output:
2022
20.23
The toString(datatype value) Method
This method is used to convert the Wrapper object or primitive to String. The syntax is:
<String> <variable>= <Wrapper class>.toString(datatype variable);
Let us see the below example:
int num=2022;
String str=Integer.toString(num);
//converts 2022 to "2022"
double dou=20.22;
String str1=Double.toString(dou);
//converts "20.22" to 20.22
System.out.println(str);
System.out.println(str1);
Output:
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.
268268 Touchpad Computer Science-XI

