Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 3 of 3 FirstFirst 123
Results 51 to 57 of 57

Thread: Program to find the number of occurrences of words in the text file

  1. #51
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Program to find the number of occurrences of words in the text file

    The n or k variable should be named for what it contains. Is it something like the minimumCount to show?
    we need to count the repeated words.
    How will the code do that? What steps will it take to do the counting?
    Some earlier code used the Collections class's frequency() method? Can that be used?
    If you don't understand my answer, don't ignore it, ask a question.

  2. #52
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Program to find the number of occurrences of words in the text file

    As per previous replies, nested for-loop needs modification for calculating the occurance of words in a file.

    Once you get all the words from file, try using either a map or java class (i.e. haivng 2 members as WORD and COUNT) to store word and its count. This would be easy for writing the logic.

    If you want to calculate the words of same root (i.e. words starting with a fixed character set. Ex- TALL, TALLer, TALLest), then you can try Regular Expression. There can be other better approach for such scenario. Please could you clarify if I misunderstood you?
    Thanks and regards,
    Sambit Swain

  3. #53
    Member
    Join Date
    Dec 2013
    Posts
    32
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Program to find the number of occurrences of words in the text file

    Quote Originally Posted by Sambit View Post
    As per previous replies, nested for-loop needs modification for calculating the occurance of words in a file.

    Once you get all the words from file, try using either a map or java class (i.e. haivng 2 members as WORD and COUNT) to store word and its count. This would be easy for writing the logic.

    If you want to calculate the words of same root (i.e. words starting with a fixed character set. Ex- TALL, TALLer, TALLest), then you can try Regular Expression. There can be other better approach for such scenario. Please could you clarify if I misunderstood you?
    Yes you are right you have understood my need correctly here is my updated code still i am confused about using Regular Expression.
    import java.io.*;
    import java.util.*;
     
    class FrequencyCounter {
     
        public static void main(String[] args) {
            System.out.println("Enter the file path to analyse:");
            Scanner scan = new Scanner(System.in);
            String path = scan.nextLine();//Files present in this path will be analysed to count frequency
            File directory = new File(path);
            File[] listOfFiles = directory.listFiles();//To get the list of file-names found at the "directoy"
            BufferedReader br = null;
            String words[] = null;
            String line;
            String files;
            Map<String, Integer> wordCount = new HashMap<String, Integer>();
            for (File file : listOfFiles) {
                if (file.isFile()) {
                    files = file.getName();
                    if (files.endsWith(".txt") || files.endsWith(".TXT")) {
                        try {
                            br = new BufferedReader(new FileReader(files));
                            try {
                                while ((line = br.readLine()) != null) {
                                    line = line.toLowerCase();
                                    words = line.split("\\s+");
                                }
                            } catch (NullPointerException e) {
                                System.out.println("I could'nt read your files:" + e);
                            }
                        } catch (IOException e) {
                            System.out.println("I could'nt read your files:" + e);
                        }
                    }
                }
            }
     
            for (String read : words) {
     
                Integer freq = wordCount.get(read);
                wordCount.put(read, (freq == null) ? 1 : freq + 1);
            }
     
            System.out.println(wordCount.size() + " distinct words:");
            System.out.println(wordCount);
        }
    }

  4. #54
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Program to find the number of occurrences of words in the text file

    Does this new program follow the steps that were set out for the previous program? For example it does not make a list of all the words in all the files.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #55
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Program to find the number of occurrences of words in the text file

    Now also posted here.

  6. #56
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program to find the number of occurrences of words in the text file

    What about how to search in text file any word and count how many it were repeated? Can try this one...
    public int countWord(String word, File file) {
    int count = 0;
    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
    String nextToken = scanner.next();
    if (nextToken.equalsIgnoreCase(word))
    count++;
    }
    return count;
    }

  7. #57
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Program to find the number of occurrences of words in the text file

    Dinesh,

    You were 90% close to the solution.

    There are 2 modification required to achieve the solution:

    1. Use a nested loop to check the frequecy of the words present
    2. Use the below code to calculate the frequency:

    if(each.indexOf(word) >= 0) {
    count ++;
    }


    Please refer to the link "http://stackoverflow.com/questions/17134773/to-check-if-string-contains-particular-word" if you want to use regular expression.

    Thanks,
    Thanks and regards,
    Sambit Swain

Page 3 of 3 FirstFirst 123

Similar Threads

  1. [SOLVED] how to find all occurrences of a number in an array recursivelly
    By mia_tech in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 13th, 2012, 01:37 PM
  2. Reading text file and counting the number of words, integers, and floats.
    By Jsmooth in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: April 12th, 2012, 06:39 PM
  3. [SOLVED] Replacing words in a text file
    By sanelko in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: January 30th, 2012, 01:56 AM
  4. A program that counts the number of punctuation marks in a text file
    By Twoacross in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2011, 04:03 PM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM

Tags for this Thread