
how do i extract words from strings in java???

Hi Friend,
Either you can use split() method:
import java.util.*;
class ExtractWords
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.print("Enter String: ");
String st=input.nextLine();
String str[]=st.split(" ");
for(int i=0;i<str.length;i++){
System.out.println(str[i]);
}
}
}
Or you can use StringTokenizer class:
import java.util.*;
class ExtractWords
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.print("Enter String: ");
String str=input.nextLine();
StringTokenizer st=new StringTokenizer(str);
while(st.hasMoreTokens()){
String token=st.nextToken();
System.out.println(token);
}
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.