REA98 REA98 - 6 months ago 26
Java Question

Asking for a word and checking its number of occurrence in a text file

public class Try{
public static void main (String args[]) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("Try.txt"));
Scanner sc = new Scanner(System.in);
System.out.print("Enter the subtring to look for: ");
String Word=sc.next();
String line=in.readLine();
int count =0;
String s[];
do
{
s=line.split(" ");
for(int i=0; i < s.length; i++)
{
String a = s[i];
if(a.contains(Word))
count++;
}
line=in.readLine();
}while(line!=null);
System.out.print("There are " +count+ " occurences of " +Word+ " in ");
java.io.File file = new java.io.File("Try.txt");
Scanner input = new Scanner(file);
while(input.hasNext())
{
String word = input.nextLine();
System.out.print(word);
}

}
}


The intended purpose of my program is to ask the user for a certain word(s) that will be checked in a text file and if it exists, it will count the number of times the user-entered word occurs in the text file. So far, my program can only search for one word. If I try two words separated by space, only the first word will be searched and counted for its number of occurrence. Any tips on how to search multiple words?

Answer

I was following literally the title of the question and therefore I will suggest this algorithm:

public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new FileReader("Test.txt"));
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the subtring to look for: ");
    String word = sc.next();
    String line = in.readLine();
    int count = 0;
    // here is where the efficiently magic happens
    do {
    // 1. you dont need to split a line by spaces, too much overhead...
    // 2. and you dont need to do counter++
    // 3. do instead: calculate the number of coincidences that the word is
    //repeated in a whole line...that is what the line below does..
        count += (line.length() - line.replace(word, "").length()) / word.length();
    //the rest looks fine

    //NOTE: if you need a whole word then wrap the input of the user and add the empty spaces at begin and at the end...so the match will be perfect to a word

        line = in.readLine();
    } while (line != null);
    System.out.print("There are " + count + " occurences of " + word + " in ");
    }

Edit:

if you want to check more than one word in the document then use this

public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new FileReader("Test.txt"));
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the subtring to look for: ");
    String input = sc.nextLine();
    String[] word = input.split(" ");
    String line = in.readLine();
    int count = 0;
    do {
        for (String string : word) {
            count += (line.length() - line.replace(string, "").length()) / string.length();
        }

        line = in.readLine();
    } while (line != null);
    System.out.print("There are " + count + " occurences of " + Arrays.toString(word) + " in ");
}
Comments