Switch to full style
General Java code examples
Post a reply

Breaking the String into Words

Tue Nov 11, 2008 10:44 pm

Breaking the String into Words
Code:
import java.io.*;
import java.util.*;

public class BreakStringToWord{
  String str;
  int count = 0;
  public static void main(String[] args) {
    BreakStringToWord c = new BreakStringToWord();
  }
  public BreakStringToWord(){
    try{
      InputStreamReader ir = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(ir);
      System.out.print("Enter String: ");
      str = br.readLine();
      System.out.println("Enter String is : "+ str);
      System.out.println("Breaking this string is : ");
      StringTokenizer token = new StringTokenizer(str);
      while (token.hasMoreTokens()){
        count++;
        str = token.nextToken();
        System.out.println(str);
      }
      System.out.println("Number of Words : "+ count);
    }
    catch(IOException e){}
  }
}




Re: Breaking the String into Words

Sat Jan 14, 2012 10:59 am

What a joy to find such clear tihinnkg. Thanks for posting!

Post a reply
  Related Posts  to : Breaking the String into Words
 get number of words in a string     -  
 Return words count in a string     -  
 Breaking a Loop     -  
 sort words in c++     -  
 probability of repeated words     -  
 Change the letter spacing of the words     -  
 check if string start with a specific sub-string in PHP     -  
 recursive string reversal- reverse string     -  
 Splitting a String Based on a Found String     -  
 check if string ends with specific sub-string in php     -  

Topic Tags

Java Strings