简体   繁体   English

从给定查询中查找所有可能的单词

[英]find all possible words from a given query

I want to make a word search java program where whenever I enter a word that contains wild cards for example: b*t 我想制作一个单词搜索Java程序,其中每当我输入包含通配符的单词时,例如:b * t

I want to find all the possible words that fit that word from a text file that I have. 我想从我拥有的文本文件中找到所有适合该词的词。

This is what I have done so far, 这是我到目前为止所做的

    Scanner cin = new Scanner(System.in);
    System.out.print("Enter a query: ");
    String input = cin.nextLine();

    try{
        String line;
        BufferedReader br = new BufferedReader(new FileReader("dictionary.txt"));
        while((line = br.readLine()) != null){
            if (input.length()==line.length()) {//show all letters in that range
               if ('*' != input.charAt(x)) {//check if letters contain '*'
                    if (line.charAt(x) == input.charAt(x)) {//check if letters match
                    System.out.print(line+"\n");//show all words that fit criteria
                }else{//skip letter
                   x=+1;
               }
                }else{//skip
                   x=+1;
               }
            }// end if
        }// end while

    }catch (IOException e){
        e.printStackTrace();
    }

I can find all the words of certain length, I'm just struggling on finding the words that fit my query. 我可以找到一定长度的所有单词,但我一直在努力寻找适合我查询的单词。

I believe that this fit your needs: 我相信这符合您的需求:

public static void main(String[] args) { 公共静态void main(String [] args){

    Scanner cin = new Scanner(System.in);
    System.out.print("Enter a query: ");
    String input = cin.nextLine();
    try{
        String line;
        BufferedReader br = new BufferedReader(new FileReader("dictionary.txt"));
        while((line = br.readLine()) != null){
            String[] words = line.split(" ");
            for(String w: words) {
                String p = "^" + input.replaceAll("\\*", ".*") + "$";
                if (w.matches(p)) {
                    System.out.println(w);
                }
            }
        }

    }catch (IOException e){
        e.printStackTrace();
    }

    System.exit(0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM