[英]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.