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 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 57

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

  1. #26
    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 GregBrannon View Post
    Don't create a new Scanner object each time you need to get input. The object scan can be used both times.
    I commented out the second scanner object "scan 1" now its not allowing me to enter an text its not listening here is the output
    Enter the number of words to find its frequency:
    3
    Enter the file path to analyse the words:
    Exception in thread "main" java.lang.NullPointerException
    	at Counter.main(Counter.java:23)

    Quote Originally Posted by GregBrannon View Post
    I see that this line will most likely cause an error each time it is run:

    if ( words[j + 1].equals( word ) )

    You're not seeing that?
    I think its not generating any error but still may be this could be the reason for not getting the output as i expect

  2. #27
    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

    Can you post the new version of the code that shows the change you made when you commented out a statement? The change Greg was talking about meant that the code would use just one Scanner object. How does the new code do that?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    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 Norm View Post
    Can you post the new version of the code that shows the change you made when you commented out a statement? The change Greg was talking about meant that the code would use just one Scanner object. How does the new code do that?
    import java.io.*;
    import java.util.*;
     
    class Counter {
     
        public static void main(String[] args) throws NullPointerException {
            System.out.println("Enter the number of words to find its frequency:");
            Scanner scan = new Scanner(System.in);
            int k = scan.nextInt();//Number of top words to be found is stored here
            //Scanner scan1 = new Scanner(System.in);
            System.out.println("Enter the file path to analyse the words:");
            String path = scan.nextLine();// Directory path will be stored here
            File folder = new File(path);
            File[] listOfFiles = folder.listFiles();//To get the list of file-names present in the path 
            BufferedReader br = null;
            String words[] = null;
            String line = "";
            String files;
            String word;
            int count = 0;
            // List<String> list = new ArrayList<String>();
            for (File f : listOfFiles) {
                if (f.isFile()) {
                    files = f.getName();
                    if (files.endsWith(".txt") || files.endsWith(".TXT")) {
                        try {
                            br = new BufferedReader(new FileReader(f));
                            try {
                                while ((line = br.readLine()) != null) {
                                    line = line.toLowerCase(); // convert to lower case 
                                    words = line.split("\\s+");
                                    System.out.println("an ID "+ java.util.Arrays.toString(words));
                                }
                                java.util.Arrays.sort(words);
     
                                for (int i = 0; i < words.length; i++) {
                                    word = words[i];
                                    for (int j = 0; j < words.length; j++) {
                                        if (words[j + 1].equals(word)) {
                                            count++;
                                        }
     
                                    }
                                }
                                System.out.println(count);
     
                            } catch (IOException e) {
                                System.out.println("I am sorry:" + e);
                            }
                        } catch (FileNotFoundException e) {
                            System.out.println("Here i have got" + e.getMessage());
                        }
     
                    }
                }
            }
        }
    }

  4. #29
    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

    You've been pushed from one problem to another. There are issues with the Scanner class when using the nextLine() method after using one of the other next methods. Either add an extra call to nextLine() after nextInt() or put it back the way it was.

    Where is the println statement that prints out the value of line?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    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

    Okay i have revoked the change in scanner object and i have added the println statement after splitting the contents by space (Line number:33)Norm

  6. #31
    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

    What happens when the code is compiled and executed? Copy the output and paste it here.

    BTW you should compile and execute the code BEFORE posting anything so the post can have all the information about the problem and not require someone to ask for it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #32
    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

    sorry i will follow this method of posting right from now thanks for being patient with my mistakes and here is the code which i am having now
    import java.io.*;
    import java.util.*;
     
    class Counter {
     
        public static void main(String[] args) throws NullPointerException {
            System.out.println("Enter the number of words to find its frequency:");
            Scanner scan = new Scanner(System.in);
            int k = scan.nextInt();//Number of top words to be found is stored here
            Scanner scan1 = new Scanner(System.in);
            System.out.println("Enter the file path to analyse the words:");
            String path = scan1.nextLine();// Directory path will be stored here
            File folder = new File(path);
            File[] listOfFiles = folder.listFiles();//To get the list of file-names present in the path 
            BufferedReader br = null;
            String words[] = null;
            String line = "";
            String files;
            String word;
            int count = 0;
            // List<String> list = new ArrayList<String>();
            for (File f : listOfFiles) {
                if (f.isFile()) {
                    files = f.getName();
                    if (files.endsWith(".txt") || files.endsWith(".TXT")) {
                        try {
                            br = new BufferedReader(new FileReader(f));
                            try {
                                while ((line = br.readLine()) != null) {
                                    line = line.toLowerCase(); // convert to lower case 
                                    words = line.split("\\s+");
                                    System.out.println("an ID "+ java.util.Arrays.toString(words));
                                }
                                java.util.Arrays.sort(words);
     
                                for (int i = 0; i < words.length; i++) {
                                    word = words[i];
                                    for (int j = 0; j < words.length; j++) {
                                        if (words[j + 1].equals(word)) {
                                            count++;
                                        }
     
                                    }
                                }
                                System.out.println(count);
     
                            } catch (IOException e) {
                                System.out.println("I am sorry:" + e);
                            }
                        } catch (FileNotFoundException e) {
                            System.out.println("Here i have got" + e.getMessage());
                        }
     
                    }
                }
            }
        }
    }
    here is the output of it :
    Enter the number of words to find its frequency:
    3
    Enter the file path to analyse the words:
    /home/dinesh/Desktop
    an ID [hi]
    an ID [hello]
    an ID [hi]
    an ID [hello]
    an ID [hi]
    an ID [norm]
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    	at Counter.main(Counter.java:40)

  8. #33
    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

    Where is the value of the line variable printed? That will show what is read from the file?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #34
    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

    i have made the code to store the contents of line variable as an string array (words) by using space as delimiter and i am printing the contents of that words array,I have not made the code as to print the contents of the line variable here is that part of code Norm.
    while ((line = br.readLine()) != null) {
                                    line = line.toLowerCase(); // convert to lower case 
                                    words = line.split("\\s+");
                                    System.out.println("an ID "+ java.util.Arrays.toString(words));
                                }

  10. #35
    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

    I have not made the code as to print the contents of the line variable h
    Why not?
    Add a call to the prinln() method to print out the value of the line variable immediately after it is assigned a value.

    The problems you are having is because you have not done a design for what the steps are that the program is supposed to do.
    Before writing any more code, make a detailed list of the steps the program needs to do to solve the problem. Those steps should be listed so we can see that the design will solve the problem.
    When the design looks good, THEN try writing the code to do the steps listed in the design.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #36
    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

    After adding the printing statement to print the line variable contents my output is as follows:
    Enter the number of words to find its frequency:
    3
    Enter the file path to analyse the words:
    /home/dinesh/Desktop
    hi
    an ID [hi]
    hello
    an ID [hello]
    hi
    an ID [hi]
    hello
    an ID [hello]
    hi
    an ID [hi]
    norm
    an ID [norm]
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    	at Counter.main(Counter.java:41)
    YES Norm you are right i haven't designed the steps anyway i am listing it below correct me if i go wrong here
    1)When the user runs my application he must be prompted to enter the number say "n" which is used for pointing top "n" occurring words (i.e.,the words with higher frequency if n=5 then the code must display the 5 highly frequent words).

    2)The code must prompt the user to enter the path from which the text files has to be read and find the frequency of words.

    3)I am storing the contents of the file as an string array since we i can use Arrays.sort() method and sort its contents so it will be easy for me to find the frequency using indexof() method.<Note:This is not yet implemented>

    4)Using looping to add each of the contents of the array and using equals() method to check whether the values are same and if so the count will be incremented.

    5)Now after all the checking and counting i need to display the output as word:count in decreasing order of count value<Note:Not yet Implemented>

  12. #37
    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 first two steps are simple and don't require much logic. The other steps must be expanded to cover all the details:
    storing the contents of the file as an string array
    What are the steps the program must take to do that? What is in the file? What are on the lines that are read? What is stored in the array?
    Can you give a small example of the contents of a file and show how it will be stored in the array when this step is finished?

    check whether the values are same and if so the count will be incremented.
    How does the code compare the contents of the array? What elements are compared to what others?
    What should the code be counting?
    Post a sample array contents and show what the count(s) should be for that sample.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #38
    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

    Norm,here i have modified the code now i have decided to read the file contents and add it to an List(ArrayList) by splitting the contents by space and i am using the inbuilt function of collection (sort) to sort the contents of the array list and i am trying to use the frequency function to count the frequency of words here is the updated version of code
    import java.io.*;
    import java.util.*;
     
    class Counter {
     
        public static void main(String[] args) throws NullPointerException {
            System.out.println("Enter the number of words to find its frequency:");
            Scanner scan = new Scanner(System.in);
            int k = scan.nextInt();//Number of top words to be found is stored here
            Scanner scan1 = new Scanner(System.in);
            System.out.println("Enter the file path to analyse the words:");
            String path = scan1.nextLine();// Directory path will be stored here
            File folder = new File(path);
            File[] listOfFiles = folder.listFiles();//To get the list of file-names present in the path 
            BufferedReader br = null;
            String words[] = null;
            String line = "";
            String files;
            String word;
            int count = 0;
            List<String> list = new ArrayList<String>();
            for (File f : listOfFiles) {
                if (f.isFile()) {
                    files = f.getName();
                    if (files.endsWith(".txt") || files.endsWith(".TXT")) {
                        try {
                            br = new BufferedReader(new FileReader(f));
                            try {
                                while ((line = br.readLine()) != null) {
                                    line = line.toLowerCase(); // convert to lower case.
                                    words = line.split("\\s+");
                                    list.addAll(java.util.Arrays.asList(words));
                                    java.util.Collections.sort(list);
                                    System.out.println(list);
                                    /*for (String read : list) {
                                     System.out.println(Collections.frequency(list, read) + ":" + list);
                                     }*/
                                }
     
                                for (int i = 0; i < words.length; i++) {
                                    word = words[i];
                                    for (int j = 0; j < words.length; j++) {
                                        if (words[j + 1].equals(word)) {
                                            count++;
                                        }
     
                                    }
                                }
                                System.out.println(count);
     
                            } catch (IOException e) {
                                System.out.println("I am sorry:" + e);
                            }
                        } catch (FileNotFoundException e) {
                            System.out.println("Here i have got" + e.getMessage());
                        }
     
                    }
                }
            }
        }
    }
    But still the code is not working as i expected here is the latest output:
    Enter the number of words to find its frequency:
    3
    Enter the file path to analyse the words:
    /home/dinesh/Desktop
    [hello, hello, hi, hi, hi, norm]
    [, hello, hello, hi, hi, hi, norm]
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    	at Counter.main(Counter.java:44)
    The statement "if (words[j + 1].equals(word))" is causing the error but if the new version is good i will remove this statements but please help me to fix the code.[COLOR="Silver"]

  14. #39
    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

    If the statement:

    if (words[j + 1].equals(word))

    is causing the error (as it was in your first post), then ensure that j + 1 does not exceed the array's number of elements - 1. How could you do that? By reducing what j can be by 1. Change the upper limit of j in the for loop statement.

  15. #40
    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

    Can you finish writing down the steps the code is supposed to do after the ? Decide on the list of steps BEFORE writing the code.
    What steps need to be taken after the contents of the file has been read into the ArrayList?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #41
    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 GregBrannon View Post
    If the statement:

    if (words[j + 1].equals(word))

    is causing the error (as it was in your first post), then ensure that j + 1 does not exceed the array's number of elements - 1. How could you do that? By reducing what j can be by 1. Change the upper limit of j in the for loop statement.
    now i have replaced j+1 with j now no exception is thrown.
    Kindly check my previous post i have modified the logic now using ArrayList and Collections.frequency function to count the frequency of the words but still its not working perfectly.

  17. #42
    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

    And what does "not working perfectly" mean? Show a sample run as you've done before and what is incorrect with it.

  18. #43
    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

    You need to stop writing code and complete the design steps first. Writing code without a design is often a waste of time.
    Make a list of the steps the code needs to do.
    How about this case:
    When should the words be counted? After each line is read or after all the files for the whole file is read?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #44
    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

    Can you see that the correct answer is there, but the output is "wrong"? And the output isn't really wrong, but it includes test code that you used to improve your program. I think you can remove the undesired output and then modify the actual output so that it produces what you need.

  20. #45
    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 words should be counted only after reading and storing the contents of the file(.txt)
    It's not clear yet when the counting is supposed to be done:
    Is the counting supposed to be done AFTER ALL the files have been read and all their contents stored in the arraylist?

    i have made the design
    Please post the steps in the design. What you have posted so far is too high level to use to write the code.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #46
    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 Norm View Post
    It's not clear yet when the counting is supposed to be done:
    Is the counting supposed to be done AFTER ALL the files have been read and all their contents stored in the arraylist?


    Please post the steps in the design. What you have posted so far is too high level to use to write the code.
    Here is the design
    1)The application must prompt the user to enter the number "n" which is the top frequent words if n=2 the code must show the top two words with largest count as a result.

    2)The user must be prompted to enter the file path from which the (.txt) files will be read.

    3)The code must read all the .txt files one by one(if there exist many files) and addall the contents of those files into the ArrayList and they has to be splitted based on the space as delimiter.

    4)Now the contents of the ArrayList has to be sorted using collection.sort() method.

    5)Now the frequency of all the words in the ArrayList has to be computed using collection.frequency() method.

    6)If n=2 then the two words with highest count will be printed in the console and the program needs to be terminated.

    These are the steps i need to implement i am sorry i couldnt express the design i think

  22. #47
    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 code must read all the .txt files one by one(if there exist many files) and addall the contents of those files into the ArrayList
    Does that say that ALL the words from all the files are saved in the arraylist BEFORE the arraylist is sorted and searched?
    frequency of all the words in the ArrayList has to be computed using collection.frequency() method.
    Where is the collection class defined? There is a Collection class in java SE. The spelling is important: c is not the same as C.

    If n=2 then the two words with highest count will be printed in the console
    What is the variable n? How is it given a value? Where is it stored?
    If you don't understand my answer, don't ignore it, ask a question.

  23. #48
    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 Norm View Post
    Does that say that ALL the words from all the files are saved in the arraylist BEFORE the arraylist is sorted and searched?
    Yes it must read ALL THE WORDS from ALL the files present in the path and add them all to the ArrayList BEFORE sorting.

    Where is the collection class defined? There is a Collection class in java SE. The spelling is important: c is not the same as C.
    I am sorry forgot to press "Shift" while typing "C" yes its built-in class "Collection"
    What is the variable n? How is it given a value? Where is it stored?
    I used the variable "k" instead of "n" in the code i have used Scanner object "scan" to get the input from the user and its value will be assigned to the variable "k".

  24. #49
    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

    I used the variable "k" instead of "n"
    When describing the steps the program should take, use common words to describe a value, not a program variable name. What values does "n" hold? The frequency count of the current word? The number of files read?

    read ALL THE WORDS from ALL the files present in the path and add them all to the ArrayList BEFORE sorting
    That sounds ok, does the code do that now?

    Since the arraylist contains all the words from all the files, what will the counting code do about repeated words?

    What is the program's output now?
    If you don't understand my answer, don't ignore it, ask a question.

  25. #50
    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 Norm View Post
    When describing the steps the program should take, use common words to describe a value, not a program variable name. What values does "n" hold? The frequency count of the current word? The number of files read?
    The variable "n" has been replaced by "k".
    k is used for storing an number which says the top occurring words i mean if hello:4,hi:3,norm:1 and the user enters 2 for "k" then hello:4,hi:3 is only displayed based on only the count of the words.

    Since the arraylist contains all the words from all the files, what will the counting code do about repeated words?

    What is the program's output now?
    The code is to find the occurrences of the words in the ArrayList so we need to count the repeated words.

Page 2 of 3 FirstFirst 123 LastLast

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