This discussion is archived
1 2 Previous Next 17 Replies Latest reply: Oct 31, 2012 3:57 AM by 969679 RSS

Search word in text file

969679 Newbie
Currently Being Moderated
Hello,
someone can help me with code?
How to search in text file any word and count how many it were repeated?

For example test.txt:
hi
hola
hey
hi
bye
hoola
hi
And if I want to know how many times are repeated in test.txt word "Hi" program must say "3 times repeated"

I hope you understood what I want, thank you for answers.
  • 1. Re: Search word in text file
    EJP Guru
    Currently Being Moderated
    You won't get answers to a question like that. You will only get more questions. Such as: what have you tried? Where's your code? Is it the I/O or the search that you're having trouble with?
  • 2. Re: Search word in text file
    969679 Newbie
    Currently Being Moderated
    This is my code:
    package lt.kvk.i3_2.test;
    
    import java.io.*;
    
    public class Paieska2 {
        public static void main(String args[]) {
            try {
                String stringSearch = "word";
                // Open the file c:\test.txt as a buffered reader
                BufferedReader bf = new BufferedReader(new FileReader("src/lt/kvk/i3_2/test/test.txt"));
    
                // Start a line count and declare a string to hold our current line.
                int linecount = 0;
                    String line;
    
                // Let the user know what we are searching for
                System.out.println("Searching for " + stringSearch + " in file...");
    
                // Loop through each line, stashing the line into our line variable.
                while (( line = bf.readLine()) != null){
                    // Increment the count and find the index of the word
                    linecount++;
                    int indexfound = line.indexOf(stringSearch);
    
                    // If greater than -1, means we found the word
                    if (indexfound > -1) {
                        System.out.println("Word was found at position " + indexfound + " on line " + linecount);
                    }
                }
    
                // Close the file after done searching
                bf.close();
            }
            catch (IOException e) {
                System.out.println("IO Error Occurred: " + e.toString());
            }
        }
    }
    But I need: Enter word by keyboard (which I want to find), count number of that word in file, this code not so good solution for me.

    Edited by: 966676 on 2012.10.20 23.36
  • 3. Re: Search word in text file
    EJP Guru
    Currently Being Moderated
    this code not so good solution for me
    Why not?
  • 4. Re: Search word in text file
    969679 Newbie
    Currently Being Moderated
    I dont know if this good. How to make this program to count number of specific word in file? And I need enter word by keyboard

    Edited by: 966676 on 2012.10.22 00.10
  • 5. Re: Search word in text file
    EJP Guru
    Currently Being Moderated
    How to make this program to count number of specific word in file?
    Err, increment a counter every time you find one?
    And I need enter word by keyboard
    And your difficulty with that is what? Are you aware of System.in?
  • 6. Re: Search word in text file
    969679 Newbie
    Currently Being Moderated
    EJP wrote:
    How to make this program to count number of specific word in file?
    Err, increment a counter every time you find one?
    How to make it?
  • 7. Re: Search word in text file
    gimbal2 Guru
    Currently Being Moderated
    966676 wrote:
    EJP wrote:
    How to make this program to count number of specific word in file?
    Err, increment a counter every time you find one?
    How to make it?
    int counter = 0;
    
    counter = counter + 1; // adds one
    counter++; // adds one
    counter += 1; // adds one
    
    System.out.println("Counter is now: " + counter);
    Pick your favorite way to add one to a specific value. You want to do that when you find a specific word...
  • 8. Re: Search word in text file
    EJP Guru
    Currently Being Moderated
    Err, increment a counter every time you find one?
    How to make it?
    You are already incrementing a counter for every line read from the file. The process of incrementing a counter for every match is remarkably similar.

    Did you write this code? Do you know anything about computer programming? If not, this is not the place to learn.
  • 9. Re: Search word in text file
    969679 Newbie
    Currently Being Moderated
    I don't know why, but counter all the time show: 3, whats wrong?
  • 10. Re: Search word in text file
    EJP Guru
    Currently Being Moderated
    What's wrong with what? This is a forum, not a mind reading service. Code?
  • 11. Re: Search word in text file
    gimbal2 Guru
    Currently Being Moderated
    EJP wrote:
    What's wrong with what? This is a forum, not a mind reading service. Code?
    If he's talking about the snippet I posted I'm going to move to another universe.
  • 12. Re: Search word in text file
    aksarben Journeyer
    Currently Being Moderated
    Be sure to send us some nice postcards. I've always wondered what other universes are like!
  • 13. Re: Search word in text file
    969679 Newbie
    Currently Being Moderated
    This is my code now.
    package lt.kvk.i3_2.test;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    
    
    public class Testing {
    
    
              public static void main(String args[]) throws Exception {
                   Scanner input = new Scanner(System.in);
                System.out.println("Select word from list:");
                System.out.println();
    
                try {
                        FileReader fr = new FileReader("src/lt/kvk/i3_2/test/List.txt"); // this is list of words, everything all right here
                        BufferedReader br = new BufferedReader(fr);
                        String s;
                        while((s = br.readLine()) != null) {
                             System.out.println(s);
                        }
                        fr.close();
                        String stilius = input.nextLine();   // eneter word which I want to count in File.txt
                    BufferedReader bf = new BufferedReader(new FileReader("src/lt/kvk/i3_2/test/File.txt")); // from this file I need to count word which I entered before
                   
                    int counter = 0;                
                    counter = counter + 1; 
                    counter++; 
                    counter += 1;
                    String line;
                    
                    System.out.println("Looking for information");
                    while (( line = bf.readLine()) != null){
                    int indexfound = line.indexOf(stilius);
                    if (indexfound > -1) {
                    System.out.println( "At all words which You want to count are "+ counter);
                    }
                    bf.close(); 
                    }
                   
                }
                catch (IOException e) {
                    System.out.println("Error");
                    }
                }
              }         
    Here example of file.txt:
    test
    tea
    tree
    
    test
    car
    wind
    
    dog
    test
    car
    
    sea
    tea
    test
    If I enter for example word "test" or any from this list I got answer:
    Looking for information
    At all words which You want to count are 3
    But it must to count how many times in this file this word are repeated, it must be 4
    if I enter word car It must show 2....

    And if I enter word which isn't in list (file.txt)
    Program didn't show error, just:
     
    Looking for information
    Thanks for answers.
  • 14. Re: Search word in text file
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Fine, you coped gimbals demonstration without any thought.

    Then do you know what the line containing the word <tt>while</tt> does? I think you don't, because you probably want the line
    System.out.println( "At all words which You want to count are "+ counter);
    executed only once...

    And just because I'm curios: does your file <tt>src/lt/kvk/i3_2/test/File.txt</tt> start with an empty line or does it have any content at all?

    bye
    TPD
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points