Finding instances of a small word often contained in other words

I am trying to write a file search algorithm. In this case for example my program recognizes the string whether it is inside another word. For example If i want to search for "man" i will get and other instances like "catman" or "manipulate", thing that i don't want.

package threadedsearch;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Scanner;

public class ThreadedSearch {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); //antikeimeno scanner

        // parakatw pairnoume to input apo to xrhsth

        System.out.print("Target:"); 
        String Target = scanner.next();
        System.out.print("Key:");
        String key= scanner.next();

        //prospatheia anoigmatos tou arxeiou
        int appearances = 0;
        int index=0;
        //File file= new File(Target); //antikeimeno gia to arxeio

            try {

             BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(Target),"UTF-8"));
             LineNumberReader lr=new LineNumberReader(br);
             String line;

              while ((line = lr.readLine()) != null)
              {
                  index++;
                  if (line.matches(key)) {
                    System.out.println("Found at line" + index );
                  }
                  else{
                    System.out.println("To arxeio de vrethike"); //not found
                  }
              }
            } catch (FileNotFoundException e) {
                System.out.println("To arxeio de vrethike");
                e.printStackTrace();

            } catch (IOException e) {

            }



    }

Upvotes: 1

Views: 61

Answers (2)

Mark Melgo
Mark Melgo

Reputation: 1478

Edit your code inside the while loop:
Note that this is just a rough solution, it can be improved.

while ((line = lr.readLine()) != null) {
    index++;
    boolean found = false;
    if (!line.contains (" "+key+" ")) { // check if the line contains key, if not then retrieve the words in the line
        String[] words = line.split(" "); // split lines by space to get the words in the line
        for (String word : words) { //iterate each word
            if (!Character.isLetterOrDigit(word.charAt(word.length())) && word.charAt(word.length()-1) != '"') { // check if the last character of the word is not a letter/number (e.g. "sample."), the second condition is for words inside a qoutation
                word = word.substring(0, word.length() -1); // remove the last character
            }

            // The checking below is used for words that are inside a quotation marks
            if (word.charAt(0) == '"' && word.charAt(word.length()-1) == '"') { // check if the first character of the word is not a letter/number 
                word = word.substring(1, word.length() - 1); // remove the first character
            }
            if (word.equals(key)) { //compare the word if it is equal to the key
                found = true; // if equal then mark found as true and go out from the loop
                break;
            }
        }
    } else {
        found = true;
    }

    if (found) {
        System.out.println("Found at line" + index );
    } else {
        System.out.println("To arxeio de vrethike"); //not found
    }
}

Upvotes: 2

Vinccool96
Vinccool96

Reputation: 268

Take inspiration from this:

public static int searchCount(File fileA, String fileWord) throws FileNotFoundException
{
    int count = 0;
    fileWord = fileWord.trim();
    Scanner scanner = new Scanner(fileA);

    while (scanner.hasNext()) // Fix issue #2
    {
        String nextWord = scanner.next().trim();
        if (nextWord.equals(fileWord)) { // Fix issue #1
            ++count; 
        }
    }
    //End While 
    return count;
}

It's easy to modify it to get what you're looking for.

Upvotes: 2

Related Questions