I look for a faster algorithm. I try to verify that a word exist in a dictionary.
Here is my code in java.
public class Searcher {
public static void main(String[] args){
File file = new File("pathToFile");
Scanner scanner = null;
try{
scanner = new Scanner(file);
}catch(FileNotFoundException e){
System.err.println("Le fichier n'a pas ete trouve");
}
//Word to look for.
String word = "mot";
//indicator of word existence.
boolean nonExistence = true;
while(scanner.hasNext()){
if(Pattern.matches(word, scanner.next())){
System.out.println("\"" + word + "\"" + " est un mot francais.");
nonExistence = false;
break;
}
}
if(nonExistence){
System.out.println("\'" + word + "\'" + " n'est pas un mot francais.");
}
}
}
I would like to do not have to explore the whole file. Thanks.
Scanner; it's very slow. Use aBufferedReader. For two, if you are not using Regex, don't use Regex - again, it adds overhead. Finally, finding a something in a set of things must beO(n)- so you will have to read the whole file, unless there are some other properties that you haven't mentioned.Pattern.matchesbyword.equals. Yet the answer will get more complex if you search substrings instead of words.