Results 1 to 5 of 5
- 05-19-2010, 08:39 AM #1
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
Count same word from many file in directory
Hi! i'm beginner in java programming. i need count same word from many files, example:
doc1 : boys like to play football
doc2 : girls play tennis
doc3 : almost all boys and girls play tennis
lets say my program read doc1 and first word is "boys", now my program will check if "boys" is also appeared in another document, if appeared, count the word "boys". And it will check second word "like" is also appeared in another document or not, if not appeared, remove that word. And i need output is the filename and the word that appeared in another document.
Any code or example? please, i really need code example.
thank's for your answer and sorry for my bad english.
- 05-19-2010, 08:47 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
What have you done so far?
- 05-19-2010, 02:04 PM #3
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
this is what i'v done but i think it didn't work
public class FWS {
public static void main(String[] args) throws Exception {
HashMap<String, Integer> wordFile = new HashMap<String, Integer>();
File directory = new File ("c:/document/");
File files[] = directory.listFiles();
for (File f : files) {
FileInputStream fis = new FileInputStream(f);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String inputText = "";
inputText = in.readLine().toLowerCase();
BufferedReader line = new BufferedReader(new FileReader(inputText));
StreamTokenizer st = new StreamTokenizer(line);
st.resetSyntax();
st.wordChars('A', 'Z');
st.wordChars('a', 'z');
int freq = StreamTokenizer.TT_WORD;
while ((freq != StreamTokenizer.TT_EOF)) {
freq = st.nextToken();
if (freq == StreamTokenizer.TT_WORD){
String token = st.sval;
if (wordFile.get(token) == null) {
wordFile.put(token, 1);
}
else {
wordFile.put(token, wordFile.get(token) + 1);
}
}
}
System.out.println("Word: " + wordFile.size());
}
}
}
it's error while read files in directory,,,
- 05-19-2010, 02:12 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
1.) You don't need to use FileInputStream. Just BufferedReader and FileReader are sufficient for reading text files.
2.) Don't use StringTokenizer. Use the String.split method instead to split the lines into arrays of words.
3.) Make sure you write down the steps before writing the program. Think about how you would do it manually and then translate that into an algorithm which will later become your program.
- 05-20-2010, 09:21 AM #5
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
count character in text file as input file
By aNNuur in forum New To JavaReplies: 7Last Post: 03-25-2010, 04:01 PM -
Count lines cointaining "word" in input file
By gwithey in forum New To JavaReplies: 5Last Post: 04-02-2009, 05:23 AM -
count occurence of word in a line of text
By sinyi88 in forum New To JavaReplies: 19Last Post: 02-28-2009, 07:37 AM -
Search a word(taken from one file) in another file and give the line as the output
By SwapnaNaidu in forum New To JavaReplies: 7Last Post: 11-19-2008, 02:09 PM -
How to get the count of all the lines in a file
By Java Tip in forum java.ioReplies: 0Last Post: 04-06-2008, 07:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks