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
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:
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
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 BeginnersHashMap/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 fileJava
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 stringJava
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 programneed
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 - FrameworkFacing 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
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 mehelp 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 ProgramDisplay 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
Search word from text file using JavaHow 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 Beginnersprogram 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 please help me in coding this given programplease
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 programplease
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 for this program - Java Beginnersplease
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 - XMLparsing
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
please help me in a java program !! - Java Beginnersplease
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 hibernatehelp 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
word program - Java Beginnersword 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
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
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