program to display frequency count of each word in a file using Hashmap, Hashset and streamtokenizer.plz help me out ..

Ads
 

program to display frequency count of each word in a file using Hashmap, Hashset and streamtokenizer.plz help me out ..

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();

}

}

View Answers

February 20, 2013 at 2:59 PM

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.


February 20, 2013 at 2:59 PM

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.


February 21, 2013 at 4:45 PM

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


February 21, 2013 at 5:00 PM

thank you all.. but can i have the solution program code by not using the concept of generic class...??


February 21, 2013 at 5:06 PM

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..

Ads









Related Tutorials/Questions & Answers:
program to display frequency count of each word in a file using Hashmap, Hashset and streamtokenizer.plz help me out ..
program to display frequency count of each word in a file using Hashmap, Hashset and streamtokenizer.plz help me out ..  import java.io.*; import...()) { // it is a HashSet word String str=iterator.next(); int count=0
Program to count the number of unique words in a file using HashMap
Program to count the number of unique words in a file using HashMap  import java.io.File; import java.io.FileNotFoundException; import java.util....[]) throws FileNotFoundException { File f = new File("C:/547647/word
Advertisements
Count instances of each word
going wrong...could anyone help to put me in the right direction? From my text file...Count instances of each word  I am working on a Java Project... of the words preceded by the occurrence count. My program compiles and runs
it is a linux program is there anyone who can help me out with it?
it is a linux program is there anyone who can help me out with it?  Write a Bash script that prompts the user for a user-ID and displays the corresponding user√Ę‚?¨‚?Ęs login name, owner√Ę‚?¨‚?Ęs name, and his/her home directory
Please help me out
Please help me out    Description* You are hired to develop a laptop... as well as perform searches on the database. Using your IDE (NetBeans... for each field. Submit your SQL statement for creating the table
help me out...!!!!
help me out...!!!!  i have to make a project on console based application in java.. so can anyone guide me with some gd ideas that can be implemented
Java Word Occurrence Example
can count the occurrences of each word in a file. In this example we will use the HashMap for putting and getting the values. This program takes the file... will demonstrate you about how to count occurrences of each word in a file. In this example
Count repetitions of every word from an input file
recorded i need to count only the url patterns like google,yahoo etc, plz help me...Count repetitions of every word from an input file  Hello..i got to know how can i count the repetitions of every word present in a specific input
Java Count word occurrence and export it to excel file
Java Count word occurrence and export it to excel file Here is an example of scanning a text file in a local drive, and count the frequency of each word in the text file or you can say count the number or occurrence of each word
tO FIND UNIQUE WORDS IN A FILE USING HASHMAP
(" "); // intialize an int array to hold count of each word counter= new int... count of each word) System.out.println(map.get(temp.toString...tO FIND UNIQUE WORDS IN A FILE USING HASHMAP  import java.util.
need help with program
need help with program  To write a program to read a set of words from a file and return the following 1)Each word in the file against its frequency 2) the frequency should be in the descending order of the frequencies
Java count frequency of words in the string
Java count frequency of words in the string. In this tutorial, you will learn how to count the occurrence of each word in the given string. String..., input validation, and file conversion. Here we are going to find the frequency
I need help on my Java code.... please please help me out!?
I need help on my Java code.... please please help me out!?  Well my...[0]='\0'; //Reading each word in file, converting upper-case->lower... in the file. Also it should be displayed on the screen. However my code doesn't display
write a program to remove the element from hashset using keyboard.
write a program to remove the element from hashset using keyboard.  write a program to remove the element from hashset using keyboard
help me
file (using the JFileChooser). The program will then allow the user to do... appropriate buttons or selecting from menu): 1) display the contents of the file 2) count and display the number of words in the file (Hint: You can use
displaying List of records from database in a jsp using ajax, onclick it should display the results ?? its urgent can u help me
display the results ?? its urgent can u help me   displaying List of records from database in a jsp using ajax, onclick it should display the results ?? its urgent can u help me
plz help me find a program
plz help me find a program  plz help..i want a source code in jsp for order processing
String file in to word file in java
String file in to word file in java  how to convert a String format word file into Ms Word file using java? please can anyone of you help me
help me
help me  please send me the java code to count the number of similar words in given string and replace that word with new one
it is a linux question is there anyone who can help me out with it?
it is a linux question is there anyone who can help me out with it?  ... script should display the sum of the first two numbers and third number. Both scripts should display their process id‚??s
java program to insert data into a file and count the number of words from the file???????
java program to insert data into a file and count the number of words from the file???????  java program to insert data into a file and count the number of words from the file
how to display duplicate elements with out using collection Frame work?
how to display duplicate elements with out using collection Frame work?  how to display duplicate elements with out using collection Frame work
Merge multiple jasper file to one word Doc using java
Merge multiple jasper file to one word Doc using java   how to Merge multiple jasper file to one word Doc using java
please help me in coding this given program
please help me in coding this given program  consider the below... are the prime numbers in ascending order write a program to find the nth term in this series. (i m unable to code this program..please provide solution
Help me to write this simple java frame program
Help me to write this simple java frame program   I want to write a Java program for.... To create a frame with 4 text fields;name,street,city and pin;with suitable labels. Also add a button "OK". After enter values
please help me in coding this given program
please help me in coding this given program  consider the below... are the prime numbers in ascending order write a program to find the nth term in this series. (i m unable to code this program..please provide solution
please help me to write a code for this program
please help me to write a code for this program   1 1 1 1 2 2 1 1 3 4 3 1 1 4 7 7 4 1
please help me to write a code for this program
please help me to write a code for this program   1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
help me in inserting data into db using hibernate
help me in inserting data into db using hibernate  How to insert data into dependent tables through hybernate   Hi Friend, Please visit the following link: http://www.roseindia.net/hibernate/insert-data-database.shtml
I really need help with this assignment question Please help me out Please
I really need help with this assignment question Please help me out Please ... searches on the database. Using your IDE (NetBeans or Eclipse), you should develop...: the current year. Also be mindful of relevant, valid data types for each field
help me
help me...using the built-in Queue class  Queues are after used to stimulate the flow of people , cars , airplanes , transactions , and so on . write a program

Ads