String Handling & Wrapper Classes
StringTokenizer:
It allows an application to break a string into tokens(words).
Example:
"Welcome to Java" is a one string and it has 3 tokens(words).
java.util.StringTokenizer
Constructor:
public StringTokenizer(String);
Methods:
public boolean hasMoreTokens();
=>It returns true if the token is present, otherwise returns false
public String nextToken();
=>It returns current token and moves the cursor to next token.
public int countTokens();
=>It returns no. of tokens.
Program to count the no. of words:
import java.util.*;
class Demo
{
public static void main(String args[])
{
String s="Welcome to Sathya Technologies";
StringTokenizer st=new StringTokenizer(s);
int n=st.countTokens();
System.out.println(n);
}
}
Program to iterate word by word in a given string:
import java.util.*;
class Demo
{
public static void main(String args[])
{
String s="Welcome to Sathya Technologies";
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}
It allows an application to break a string into tokens(words).
Example:
"Welcome to Java" is a one string and it has 3 tokens(words).
java.util.StringTokenizer
Constructor:
public StringTokenizer(String);
Methods:
public boolean hasMoreTokens();
=>It returns true if the token is present, otherwise returns false
public String nextToken();
=>It returns current token and moves the cursor to next token.
public int countTokens();
=>It returns no. of tokens.
Program to count the no. of words:
import java.util.*;
class Demo
{
public static void main(String args[])
{
String s="Welcome to Sathya Technologies";
StringTokenizer st=new StringTokenizer(s);
int n=st.countTokens();
System.out.println(n);
}
}
Program to iterate word by word in a given string:
import java.util.*;
class Demo
{
public static void main(String args[])
{
String s="Welcome to Sathya Technologies";
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}
Command Line Arguments:
Ø The arguments that are passed at the command prompt are called command line arguments.
Ø Command line arguments are received by main method only.
Ø The arguments are received string format only.
Examples:
1) class Demo
{
public static void main(String args[])
{
for(String s : args)
{
System.out.println(s);
}
}
}
2) class Demo
{
public static void main(String args[])
{
System.out.println(args[0]+args[1]);
}
}
To run the above application:
C:\> java Demo Taj Mahal
Output: TajMahal
C:\> java Demo 10 20
Output: 1020 because command line arguments are received as a string format only.
To convert string format to other formats we need wrapper classes.
Wrapper Classes:
Each of Java’s 8 primitive data types has a class and those classes are called wrapper classes because they wrap the data into an object.
List of primitive data types:
1) byte 2) short 3) int 4) long 5) float 6) double 7) char 8) boolean
List of wrapper classes:
1) Byte 2) Short 3) Integer 4) Long 5) Float 6) Double 7) Character 8) Boolean
Example:
class Demo
{
public static void main(String args[])
{
int x=Integer.parseInt(args[0]);
int y=Integer.parseInt(args[1]);
System.out.println(x+y);
}
}
To run the above application:
C:\> java Demo 10 20
Output: 30
To covert primitive type to reference type
int a=5;
Integer i=new Integer(a);
To covert reference type to primitive type:
Integer i=new Integer(5);
int x=i.intValue();
Auto boxing:
The process of converting primitive type to the corresponding reference type is known as auto boxing.
Example:
int x=5;
Integer i=x; => It is called as auto boxing
Auto unboxing:
The process of converting reference type to the corresponding primitive type is known as auto unboxing.
Example:
Integer i=new Integer(5);
int x=i; => it is called as auto unboxing.
Both auto boxing and auto unboxing features are introduced in JDK 1.5 verion in 2004.
1) Write a program to convert byte array into a String and display by using the following constructor of String class:
public String(byte[]);
2) Write a program to convert char array into a String and display by using the following constructor of String class:
public String(char[]);
3) Write a program to convert String into a byte array and display by using the following method of String class:
public byte[] getBytes();
4) Write a program to convert String into a char array and display by using the following method of String class:
public char[] toCharaArray();
5) Write a program to concatinate two strings by using the following method:
public String concat(String);
Assignment2:
1) Write a program to check whether given file is java file or not by using the following method of String class.
public boolean endsWith(String);
2) Write a program to check whether given string starts with alphabet A or not by using the following method of String class.
public boolean startsWith(String);
3) Write a program to count the no. of words in a given string by using the following constructor & method.
public StringTokenizer(String);
public int countTokens();
4) Write a program to iterate word by word in a given string by using the following constructor & methods.
public StringTokenizer();
public boolean hasMoreTokens();
public String nextToken();
Assignment2:
1) Write a program to check whether given file is java file or not by using the following method of String class.
public boolean endsWith(String);
2) Write a program to check whether given string starts with alphabet A or not by using the following method of String class.
public boolean startsWith(String);
3) Write a program to count the no. of words in a given string by using the following constructor & method.
public StringTokenizer(String);
public int countTokens();
4) Write a program to iterate word by word in a given string by using the following constructor & methods.
public StringTokenizer();
public boolean hasMoreTokens();
public String nextToken();
Sir include string tokenzier topic sir
ReplyDeleteSir plz provide your entire Java videos here in this blogspot showing only some topics only sir
ReplyDeletewill omit your great writing due to this problem.
ReplyDeleteazure training
java training
salesforce training
hadoop training
mulesoft training