Monday 15 September 2014

Match to String in java

package com.swain.cell;

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

public class MatchString {
 public static void main(String arg[]) {
        String str = "hima hima sekhar swain hima";
        char ch1[]=str.toCharArray();
        HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
        for(int i=0; i<ch1.length; i++)
        {
            char ch=ch1[i];
            hm.put(ch,(hm.get(ch)==null?1:hm.get(ch)+1));
        }
        System.out.println(hm.get('a'));
        
        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("Found the word " + word);
}
System.out.println("no of word " + hms.toString());
        
    }
}

No comments:

Post a Comment