import java.io.*;
import java.util.*;
class test1
{
public static void main( String args[])throws IOException {
Console con= System.console();
String fname;
System.out.println("enter the file name"); fname=con.readLine(); File f1 = new File(fname); if(!f1.exists()) {
System.out.println("Source file doesnot exists");
System.exit(0); } FileInputStream fis = new FileInputStream(fname); int ch; StringTokenizer st = new StringTokenizer(str); int count=0; while(st.hasMoreTokens()) { HashSet Set = new HashSet(); if(Set.contains(st.nextToken())) count++; } System.out.println("no of words"+count);
fis.close();
}
}
Program : by HashSet and StringTokenizer
import java.io.BufferedReader; import java.io.File;
import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Iterator; import java.util.StringTokenizer;
public class WordCount {
public static void main(String args[]) { BufferedReader br=null; try{ br=new BufferedReader(new InputStreamReader(System.in)); //Reading file name from console System.out.println("Enter file name"); String fileNam=br.readLine(); File file=new File(fileNam); // Reading the search words from console and stored in HashSet HashSet<String> hSet=new HashSet<String>(); boolean flag=true; while(flag) { System.out.println("Enter word to search in given file(to stop words enter exit)"); String word=br.readLine().trim(); if(word.equalsIgnoreCase("exit")) break; hSet.add(word); } // checking given file is available or not if(file.exists()) { String fileContent=""; //Reading total content from file and append to fileContent variable BufferedReader bReader=new BufferedReader(new FileReader(file)); String line=null; while((line=bReader.readLine())!=null) fileContent+=line; bReader.close(); //Reading the search words from HashSet and searching each word in fileContext Iterator<String> iterator=hSet.iterator(); while(iterator.hasNext()) { // it is a HashSet word String str=iterator.next(); int count=0; // split the fileContent with space delimiter StringTokenizer tokenizer=new StringTokenizer(fileContent," "); while(tokenizer.hasMoreTokens()) { if(str.equals(tokenizer.nextToken())) count++; } System.out.println(str+" word is repeated "+count+" time in given file"); } } }catch(Exception exception) { exception.printStackTrace(); } finally { if(br!=null) try { br.close(); } catch (IOException exception) { exception.printStackTrace(); } } }
}
If you want develop above program with HashMap read the words from console based on key and value(ex:first=java) and store in HashMap and read the values from HashMap and search the words in file content.
Program : by HashSet and StringTokenizer
import java.io.BufferedReader; import java.io.File;
import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Iterator; import java.util.StringTokenizer;
public class WordCount {
public static void main(String args[]) { BufferedReader br=null; try{ br=new BufferedReader(new InputStreamReader(System.in)); //Reading file name from console System.out.println("Enter file name"); String fileNam=br.readLine(); File file=new File(fileNam); // Reading the search words from console and stored in HashSet HashSet<String> hSet=new HashSet<String>(); boolean flag=true; while(flag) { System.out.println("Enter word to search in given file(to stop words enter exit)"); String word=br.readLine().trim(); if(word.equalsIgnoreCase("exit")) break; hSet.add(word); } // checking given file is available or not if(file.exists()) { String fileContent=""; //Reading total content from file and append to fileContent variable BufferedReader bReader=new BufferedReader(new FileReader(file)); String line=null; while((line=bReader.readLine())!=null) fileContent+=line; bReader.close(); //Reading the search words from HashSet and searching each word in fileContext Iterator<String> iterator=hSet.iterator(); while(iterator.hasNext()) { // it is a HashSet word String str=iterator.next(); int count=0; // split the fileContent with space delimiter StringTokenizer tokenizer=new StringTokenizer(fileContent," "); while(tokenizer.hasMoreTokens()) { if(str.equals(tokenizer.nextToken())) count++; } System.out.println(str+" word is repeated "+count+" time in given file"); } } }catch(Exception exception) { exception.printStackTrace(); } finally { if(br!=null) try { br.close(); } catch (IOException exception) { exception.printStackTrace(); } } }
}
If you want develop above program with HashMap read the words from console based on key and value(ex:first=java) and store in HashMap and read the values from HashMap and search the words in file content.
hi friend,
please go through the link, may this will be helpful for you
http://roseindia.net/java/example/java/core/java-word-occurrence-example.shtml
thank you all.. but can i have the solution program code by not using the concept of generic class...??
moreover hashmap class should also be used.. i need a program which is encoded using all these three classes hashmap, hashset and streamtokenizer. please help me out..