繁体   English   中英

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

[英]find all possible words from a given query

我想制作一个单词搜索Java程序,其中每当我输入包含通配符的单词时,例如:b * t

我想从我拥有的文本文件中找到所有适合该词的词。

这是我到目前为止所做的

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

我可以找到一定长度的所有单词,但我一直在努力寻找适合我查询的单词。

我相信这符合您的需求:

公共静态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