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









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
plz help me out with this problem...please write the program for me.thanxxxxx
plz help me out with this problem...please write the program for me.thanxxxxx  Write a program that will print out a hydro bill. The bill will be formatted as the example on the next page. The following information
plz help me out with this problem...please write the program for me.thanxxxxx
plz help me out with this problem...please write the program for me.thanxxxxx  Write a program that will print out a hydro bill. The bill will be formatted as the example on the next page. The following information
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... which will demonstrate you about how to count occurrences of each word in a file
HashMap/HashSet - Java Beginners
HashMap/HashSet  im working on a game, and i want to know how does the code for HashMap and HashSet work or can you give me the code that needs to be included in the game engine. I have the exam on monday. Can you please help
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
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
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
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
facing problem plz help me out - Framework
Facing problem plz help me out  hi i am new to servlet i deployed... the web.xml file too parallel to the classes folder now i am facing this problem.plz tell me what to do... error:The requested resource (Servlet servlet
plz help me find a program
plz help me find a program  plz help..i want a source code in jsp for order processing
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
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
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
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
Display the word that begins with vowel using Java Program
Display the word that begins with vowel using Java Program In this section, we are going to separate the word that begins with vowels from the specified text... characters using replaceAll() method. Then we have used StringTokenizer class
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
Search word from text file using Java
How to Search word from text file using Java In this section, we are going to search a word from the text file. For this, we have created a swing button... the user typed his/her name, it will check the name in the text file. To check
program for HashSet - Java Beginners
program for HashSet  I need a program that illustratest the concept of HashSet. can u please suggest me a way out.  Hi friend, A set... and SortedSet intefaces describe the properties of sets and the HashSet
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
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
please help me for this program - Java Beginners
please help me for this program   Write a program that asks the user for a starting value and an ending value and then writes all the integers (inclusive) between those two values. Enter Start: 10 Enter End: 14 10
parsing word xml file using SAX parser - XML
parsing word xml file using SAX parser  i am parsing word 2003's XML file using SAX.here my question is,i want to write some tag elements which... help me regarding this please. -Shree
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
to all Experts.. please help me. im using a Jcreator. - Java Beginners
to all Experts.. please help me. im using a Jcreator.  3. Write a program that asks the user to enter a word. The program will then repeat word for as many times as it has characters: Enter a word: ILOVEU ILOVEU
please help me in a java program !! - Java Beginners
please help me in a java program !!  the porgram should use...://en.wikipedia.org/wiki/Kosaraju_algorithm) there are 4 classes in my program : Vertex... and run the program" i still need to modify the method kosaraju in the class Graph
ModuleNotFoundError: No module named 'word-count'
ModuleNotFoundError: No module named 'word-count'  Hi, My Python... 'word-count' How to remove the ModuleNotFoundError: No module named 'word... have to install padas library. You can install word-count python
ModuleNotFoundError: No module named 'word-count'
ModuleNotFoundError: No module named 'word-count'  Hi, My Python... 'word-count' How to remove the ModuleNotFoundError: No module named 'word... have to install padas library. You can install word-count python
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
servlet not working properly ...pls help me out....its really urgent
servlet not working properly ...pls help me out....its really urgent  Hi, Below is the front page of my project 1)enty.jsp </form> <... dont know whether i used the correct method to display the data from database via
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
can some help out on wrting this program. - Java Beginners
can some help out on wrting this program.  Write a Java program... and the price of the order computed to your program. NOTE: The first number on the file
word program - Java Beginners
word program  HELLO MAM AND SIR I ASK YOUR HELP HOPE YOU GONNA HELP ME THANK YOU!!Design a program to search a word for letters that the user... program, depending on the data entered. This is a program that counts each time
Display JSP selected listbox and checkbox in xml-please help me
Display JSP selected listbox and checkbox in xml-please help me  Hi... first page ,it will stored in XML file and at same time ,it should display second... select something from 1st list box second will fill accordingly by database using
help me..
help me..  Design and write a class named Person and its two... constructor for each class. Draw the UML diagram for the classes. Write a test program... Registrar or Registrar). Override the toString() method in each class (except MyDate
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
Problem while using a HashMap for writing text ina RTF file. - Framework
Problem while using a HashMap for writing text ina RTF file.  Hi, I am trying to generate a RTF file using iText.jar, wherein I am taking... matched to the string then only write to the RTF file. Problem is Document writes
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
Display the data to MS word
the database(say im searching using an id) and should display it on the ms word...Display the data to MS word  i want help with displaying data... a word doc would help a lot!! thank you

Ads