Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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)      a year and 8 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();

}

}


Advertisement
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
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 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
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
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
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
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.
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
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
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
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
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
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
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
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
plz help me find a program
plz help me find a program  plz help..i want a source code in jsp for order processing
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
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
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
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
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
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
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
HashMap
HashMap  How to work hashmap and hashset internally
Advertisements
 

 

 

DMCA.com