naveenm_006
asked on
How to read text file in java and count the no of repeated words?
How to read text file in java and count the no of repeated words?
Regards,
Naveen.
Regards,
Naveen.
you can use the following to read the words
http://helpdesk.objects.com.au/java/using-scanner-to-read-words-from-text-file
then use a Map<String, Integer> to store the word counts
http://helpdesk.objects.com.au/java/using-scanner-to-read-words-from-text-file
then use a Map<String, Integer> to store the word counts
ASKER
please find attached text file.
tokens.txt
tokens.txt
ASKER
yes you are absolutely correct.Can give the sample code.
it's very urgent.
Regards,
Naveen
it's very urgent.
Regards,
Naveen
ASKER
it should read line by line.
This should work, but I haven't yet tested it:
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;
public class CountWords3 {
public CountWords3(){
ArrayList aa = new ArrayList();
Hashtable h = new Hashtable();
try {
DataInputStream in = new DataInputStream(new FileInputStream("C:\\temp\\text.txt"));
String buff;
while((buff=in.readLine()) != null)
{
StringTokenizer t = new StringTokenizer(buff,",");
while(t.hasMoreTokens()){
String s = t.nextToken().trim();
if(!aa.contains(s))aa.add(t.nextToken());
if(h.get(s) != null){
Integer n = (Integer)h.get(s);
h.put(s, new Integer(n.intValue() +1));
} else
h.put(s, new Integer(1));
}
}
in.close();
}catch(Exception ex) {
System.out.println("Errorr");
}
for(int j=0; j<aa.size(); j++){
String s = (String) aa.get(j);
Integer n = (Integer) h.get(s);
System.out.println(s + " " + n.intValue();
}
}
public static void main(String [] args ){
new CountWords3();
}
}
This is working and tested.
Reading from file c:\\temp\\test\\text5.txt
Reading from file c:\\temp\\test\\text5.txt
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;
public class CountWords3 {
public CountWords3(){
ArrayList aa = new ArrayList();
Hashtable h = new Hashtable();
try {
DataInputStream in = new DataInputStream(new FileInputStream("C:\\temp\\test\\text5.txt"));
String buff;
while((buff=in.readLine()) != null)
{
StringTokenizer t = new StringTokenizer(buff,",");
while(t.hasMoreTokens()){
String s = t.nextToken().trim();
if(!aa.contains(s))aa.add(s);
if(h.get(s) != null){
Integer n = (Integer)h.get(s);
h.put(s, new Integer(n.intValue() +1));
} else {
// System.out.println(" s" + s);
h.put(s, new Integer(1));
}
}
}
in.close();
}catch(Exception ex) {
System.out.println("Errorr");
}
for(int j=0; j<aa.size(); j++){
String s = (String) aa.get(j);
// System.out.println("ss " + s);
Integer n = (Integer) h.get(s);
System.out.println(s + " " + n.intValue());
}
}
public static void main(String [] args ){
new CountWords3();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Homework done then?
for_yan has given a complete answer and should receive the points
Thanks a lot, greisch, I really appreciate your
kind attention.
kind attention.
Do you mean you have certain words and you need to calculate their occurrence
in the text in the file?