查找通常包含在其他单词中的一个小单词的实例

时间:2019-03-06 00:49:34

标签: java

我正在尝试编写文件搜索算法。例如,在这种情况下,我的程序将识别字符串是否在另一个单词中。例如,如果我要搜索“ man”,我会得到,例如“ catman”或“ manipulate”之类的其他实例,则是我不需要的东西。

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) {

            }



    }

2 个答案:

答案 0 :(得分:2)

在while循环内编辑代码:
请注意,这只是一个粗略的解决方案,可以对其进行改进。

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
    }
}

答案 1 :(得分:1)

从中汲取灵感:

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;
}

很容易对其进行修改以获取所需的内容。

相关问题