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

问题描述 投票:1回答:2

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

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

            }



    }
java
2个回答
2
投票

从中获取灵感:

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

可以很容易地修改它以获得您正在寻找的东西。


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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.