Monday, August 20, 2012

How to count duplicate words from a sentence in java



Output :

no of word {android=2, java=3, example=1}

DuplicateString.java

package com.swain.cell;

import java.util.HashMap;
import java.util.StringTokenizer;

public class DuplicateString {

       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO Auto-generated method stub

        String str = "java java android android java example";
        
        StringTokenizer t = new StringTokenizer(str);
        HashMap<String,Integer> hms=new HashMap<String,Integer>();
       
              while(t.hasMoreTokens())
              {
                    
                     String word = t.nextToken();
                      hms.put(word,(hms.get(word)==null?1:hms.get(word)+1));
             
              }
              System.out.println("no of words " + hms.toString());
       
   
       }

}

Java Quick Program

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