Monday, August 20, 2012

How to count word in string using java



Output:
Number of words are: 6

CountWords.java

package com.swain.cell;

import java.util.StringTokenizer;

public class CountWords {

       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO Auto-generated method stub
        String st="how to count word in java?";
        int count=0;
        StringTokenizer stk=new StringTokenizer(st," ");
        while(stk.hasMoreTokens()){
            String token=stk.nextToken();
            count++;
        }
        System.out.println("Number of words are: "+count);

       }

}

Java Quick Program

1. Write a Code to generate Random numbers public class GenerateRandomNumbers { public static void main(String[] args) { System.out...