Program to count the number of unique words in a file using HashMap
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class CountUniqueWords {
public static void main(String args[]) throws FileNotFoundException {
File f = new File("C:/547647/word.txt");
ArrayList arr=new ArrayList();
HashMap<String, Integer> listOfWords = new HashMap<String, Integer>();
Scanner in = new Scanner(f);
int i=0;
while(in.hasNext())
{
String s=in.next();
//System.out.println(s);
arr.add(s);
}
Iterator itr=arr.iterator();
while(itr.hasNext())
{i++;
listOfWords.put((String) itr.next(), i);
//System.out.println(listOfWords);
}
Set<Object> uniqueValues = new HashSet<Object>(listOfWords.values());
System.out.println("The number of unique words: "+uniqueValues.size());
}
}
View Answers
Ads
Related Tutorials/Questions & Answers:
tO FIND UNIQUE WORDS IN A FILE USING HASHMAP
tO FIND
UNIQUE WORDS IN A
FILE USING HASHMAP import java.util....()));
}
System.out.println("
Number of
unique words in
file "+cc...()
{
// create a
HashMap to store
unique combination of
words
Advertisements
how to count words in string using java
how to
count words in string
using java how to
count words in string...++;
}
System.out.println("
Number of
words are: "+
count);
}
}
Thanks
Hello...
count=0;
String arr[]=st.split(" ");
System.out.println("
Number
Java Word Count - Word Count Example in Java
some strings and
program will
count the
number of characters and
number of
words... to
count the
number of
lines,
number of
words and
number of characters in the specified
file.
Program
takes the
file name as parameter and it counts the
number
Count number of "*"
Count number of "*" I have this code to
count the
number of * from a string entered. but I need to find it from an text
file. Any idea?
import...:");
String text = bf.readLine();
int
count = 0;
for (int i = 0; i
Count words in a string method?
count the length of arr which return the
number of
words in the inputted string...
Count words in a string method? How do you
Count words in a string...(" ");
System.out.println("
Number of
words : "+arr.length);
}catch(IOException e
Count number of characters in a column.
Count number of characters in a column. I just need you suggestions. Am from Biology Back ground. I just need to find out
number of characters in a column i.e, if i have a
file with text in this manner
ACGT
GCTA
GCTA
i need
Java count vowels
Java
count vowels
In this section you will learn how to
count the
number... and then you will get the
number of vowels
count.
Description of code : In this code.... to console.
Example : A Code to
count the
number of vowel in a
string
Convert Number To Words
Convert
Number To
Words
In this example, We are going to convert
number to
words.
Code... the string
representing the
number.
Here is the code of this
program