java - java - 在文本文件中,搜索单词

我已经编写了以下代码,


public static Set<String> signature2Words(String sig) throws FileNotFoundException{


 try (Scanner s = new Scanner(new File("c:/Users/Green/documents/dictionary.txt")).useDelimiter("s*-s*")) {



 String dictionary = new String(s.nextLine());


 System.out.println(dictionary);


 System.out.println(wordToSignature(dictionary));



 if (sig.compareToIgnoreCase(wordToSignature(dictionary)) == 0){


 System.out.println("match");


 }



 Set<String> set = new HashSet<>();


 set.add(dictionary);


 return set ;


 } 


}



时间:

这里不需要递归,一个简单的while()循环和break应该完成工作:


try (Scanner s = new Scanner(file).useDelimiter("s*-s*")) {



 while (s.hasNextLine()) {


 String dictionary = s.nextLine();


 System.out.println(dictionary);


 if (sig.compareToIgnoreCase(wordToSignature(dictionary)) == 0) {


 System.out.println("match");


 break;


 }


 }


 Set<String> set = new HashSet<>();


 set.add(dictionary);


 return set;


 }



...