How to count the number of times a word is displayed in a text file in Java?

So, I'm pretty new to Java and I'm working on code that has to read a file .txt

that the user enters and then prompts the user for a search word in the file.I'm .txt

having a hard time figuring out how to count the number of times the entered word appears in the file .txt

. Instead, the code I have only counts the number of lines the code appears on. Can anyone help me figure out what to do so that my program counts the number of times the word is displayed, rather than the number of lines that the word is showing? knowledgeable? Thank! Here's the code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TextSearch {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner txt;
        File file = null;
        String Default = "/eng/home/tylorkun/workspace/09.1/src/Sample.txt";

        try {
            txt = new Scanner(System.in);
            System.out.print("Please enter the text file name or type  'Default' for a default file. ");
            file = new File(txt.nextLine());

            txt = new Scanner(file);

            while (txt.hasNextLine()) {
                String line = txt.nextLine();
                System.out.println(line);
            }
            txt.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        try {
            txt = new Scanner(file);
            Scanner in = new Scanner(System.in);
            in.nextLine();
            System.out.print("Please enter a string to search for. Please do not enter a string longer than 16 characters. ");
            String wordInput = in.nextLine();

            //If too long
            if (wordInput.length() > 16) {
                System.out.println("Please do not enter a string longer than 16 characters. Try again. ");
                wordInput = in.nextLine();
            }

            //Search
            int count = 0;
            while (txt.hasNextLine()) //Should txt be in? 
            {
                String line = txt.nextLine();
                count++;
                if (line.contains(wordInput)) //count > 0
                {
                    System.out.println("'" + wordInput + "' was found " + count + " times in this document. ");
                    break;
                }
            //else
                //{
                //    System.out.println("Word was not found. ");
                //}
            }
        } catch (FileNotFoundException e) {
            System.out.println("Word was not found. ");
        }
    } //main ends
} //TextSearch ends

      

+3


source to share


2 answers


Your problem is that you are incrementing the count per line, whether or not that word is present. Also, you don't have any code to count multiple matches per line.

Instead, use a regular expression search to find matches, and the number of increments for each match found:

//Search
int count = 0;
Pattern = Pattern.compile(wordInput, Pattern.LITERAL | Pattern.CASE_INSENSITIVE);
while(txt.hasNextLine()){
    Matcher m = pattern.matcher(txt.nextLine());

    // Loop through all matches
    while (m.find()) {
        count++;
    }
}

      




NOTE. Not sure what you are using this for, but if you just need functions, you can combine the command line grep

and wc

(wordcount) utilities. See this SO answer for how to do it.

0


source


Since the word doesn't have to be self-contained, you can do an interesting loop for

to count how many times your word appears on each line.

public static void main(String[] args) throws Exception {
    String wordToSearch = "the";
    String data = "the their father them therefore then";
    int count = 0;
    for (int index = data.indexOf(wordToSearch); 
             index != -1; 
             index = data.indexOf(wordToSearch, index + 1)) {
        count++;
    }

    System.out.println(count);
}

      

Results:



6

      

So the search segment of your code might look like this:

//Search
int count = 0;
while (txt.hasNextLine()) 
{
    String line = txt.nextLine();
    for (int index = line.indexOf(wordInput); 
             index != -1; 
             index = line.indexOf(wordInput, index + 1)) {
        count++;
    }        
}

System.out.println(count);

      

0


source







All Articles