Home Answers Viewqa Java-Beginners program to display frequency count of each word in a file using Hashmap, Hashset and streamtokenizer.plz help me out ..

 
 


lovely singh
program to display frequency count of each word in a file using Hashmap, Hashset and streamtokenizer.plz help me out ..
5 Answer(s)      3 months ago
Posted in : Java Beginners

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 Pages:
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
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
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
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
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
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
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
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.
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
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
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
please help me here
please help me here  please show me how can this show the right output that i need please continue doing this program using only if else and do while... display "insufficient Amount" every after each of the choices a window like
please help me here
please help me here  please show me how can this show the right output that i need please continue doing this program using only if else and do while... display "insufficient Amount" every after each of the choices a window like
Retrieve a list of words from a website and show a word count plus a specified number of most frequently occurring words
Retrieve a list of words from a website and show a word count plus a specified... "words" form the document, and one by one, store each word as a key into a Map<... int N = 25; //the number of word/frequency pairs to print //word pattern
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
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
from eclipse not from word file. That program should show the results also. Also... the programs from eclipse not from word file. That program should show the results also... program to check all above mentioned methods by invoking it. It should display
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
Collection : HashMap Example
Collection : HashMap Example This tutorial will help you in understanding of HashMap concept. HashMap : The java.util.HashMap class implements Map interface. It gives you an unsorted, unordered Map. It provides unique keys to each
help me to solve this problem..
help me to solve this problem..  Given below is a class definition... and price for each book from the keyboard. Print out the title of the book...; } } Write a program in a class TestBook that can do the following: Read
help me to solve this question...
help me to solve this question...  Given below is a class definition... and price for each book from the keyboard. Print out the title of the book... price; } } Write a program in a class TestBook that can do the following: Read
Java Word Count - Word Count Example in Java
Java Word Count - Word Count Example in Java  ... in the specified file. Program takes the file name as parameter and it counts the number... run the program without mentioning the file name then you will have to input
Help me
Help me  HI I am using Tomcat6.0 this is the problem i got wen i run the code , i am using login.html ,login .jsp,login.java and web.xml code...;quot;text/html&quot;); PrintWriter out=response.getWriter(); String name
Collections Exercise 4 - Word Translator
, LinkedList, HashSet, TreeSet, HashMap, and TreeMap. For passing over...: An online dictionary is needed. The user will enter a word and the program will produce a list of "translations". The program begins by reading a file
Implementing a SoftReference based HashMap - Java Tutorial
into a normal HashMap, you can easily run out of memory when you access many... objects from the HashMap by looking them up using the SoftValue.key data member...Implementing a SoftReference based HashMap 2001-03-28 The Java Specialists
How to handle Transaction IN and OUT in Inventory using java
How to handle Transaction IN and OUT in Inventory using java  Hi.... The scanned data is in .CSV file, Loaded to Oracle database using Oracle SQL Developer... this in Java/J2EE. Please can anybody help me to do this or give me some skeleton
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
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
help me 3
help me 3  write aprogram in java using two dimentional array.Accept the two dimentional array from the user and perform addition and subtraction of these two arrays and display the result for each add and subtruct
Word Count
Word Count      .... To count it we are using countMatches() method... and "sub" is substring to be count.  The code of the program
frequency of a letter in a string
frequency of a letter in a string  Could someone answer in netbeans please Write a program that takes in a string and a letter from the user...;counter<third.length;counter++){ char ch= third[counter]; int count=0
Java collection HashSet
Java collection HashSet  How can we use HashSet in java program?   The hashSet class is used to create a collection and store it in a hash table. Each collection refer to a unique value. import java.util.Collections
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
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
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
JSTL: another for each and status
JSTL: another for each and status          In this program we... set the attribute by using the pageContext implicit object. 
help me
help me  i have done as u directed but nothing happens gud to me..... . i am using netbeans is that matter at all help me and take me out from trouble where to store jfree api to use it for j2ee based project
help me
help me  MY GLASSFISH SERVER NOT START PROPERLY help me... [ GNUJAXP ] in the jarfile [ C:\Program Files\Java\jdk1.7.0_02\jre\lib\ext\gnujaxp.jar... for this optional package. i'm using netbeans
Searching a word file on server in JSP - JSP-Servlet
Searching a word file on server in JSP  Seacrhing a word file on server side using JSP. I am Apache Tomcat Server. Please help me Sir
Help me
Help me  hello!!!!. i have a Excel file which includes 4 columns and somewhat 8500 rows.these rows i have printed in SQL thru Excel file and i have 1 MYSql database which includes all those 4 fields i have defined in excel file
Please help me??
Please help me??  Question_1: Using one-dimension array of primitive... elements. Instructions: 1. Create a new file ArrayDemo.java 2. Declare... to have 8 elements as follows:2, 3, 5, 7, 11, 13, 17, and 19 4. Display
pleasa help me?
pleasa help me?  Define a class named Circle with the following... radius value as the argument. A public method to display the Circle object... named TestCircle to test the Circle program by creating two Circle objects
please help me?
please help me?  Define a class named Circle with the following... radius value as the argument. A public method to display the Circle object... named TestCircle to test the Circle program by creating two Circle objects
read XML file and display it using java servlets
read XML file and display it using java servlets  sir, i can't access... me the things where i went wrong java servlet program protected void... out = response.getWriter(); rs = search_pass.execSQL("Select * from
word and character counting - Java Beginners
word and character counting  here is the java code i made but i have to add something where it will read the inFile and display the number of words and number of characters.. can you help me with it? thanks.. :) import
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
Display JSP selected listbox and checkbox in xml-please help me
Display JSP selected listbox and checkbox in xml-please help me  Hi... ,it will stored in XML file and at same time ,it should display second page containing... from 1st list box second will fill accordingly by database using jsp. When user
Help me
Help me  HI I am using Tomcat6.0 this is the problem i got wen i run...{ response.setContentType("text/html"); PrintWriter out=response.getWriter..."); PrintWriter out=response.getWriter(); String name=request.getParameter

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.