Results 1 to 5 of 5
  1. #1
    cassiests is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

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

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    What have you done so far?

  3. #3
    cassiests is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    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,,,

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    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.

  5. #5
    cassiests is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  2. Count lines cointaining "word" in input file
    By gwithey in forum New To Java
    Replies: 5
    Last Post: 04-02-2009, 05:23 AM
  3. count occurence of word in a line of text
    By sinyi88 in forum New To Java
    Replies: 19
    Last Post: 02-28-2009, 07:37 AM
  4. Replies: 7
    Last Post: 11-19-2008, 02:09 PM
  5. Replies: 0
    Last Post: 04-06-2008, 07:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •