[英]why does my program loop infinitely?
My spell checking program doesn't give any error codes, just spell-checks the same words over and over, infinitely. 我的拼写检查程序没有给出任何错误代码,只是无限地反复检查相同的单词。 Is there a way to stop this infinite loop and put a cap on how many words it checks, for example, to end the code when ten incorrect words have been corrected? 有没有办法停止这个无限循环,并限制它检查的单词数,例如,在纠正了十个不正确的单词时结束代码?
I'm almost certain that the infinite loop is a result of this method here: 我几乎可以肯定,无限循环是此方法的结果:
public static void SpellChecker() throws IOException {
dictionary = new Hashtable<String, String>();
System.out.println("Searching for spelling errors ... ");
try {
// Read and store the words of the dictionary
BufferedReader dictReader = new BufferedReader(new FileReader("dictionary.txt"));
while (dictReader.ready()) {
String dictInput = dictReader.readLine();
String[] dict = dictInput.split("\\s"); // create an array of
// dictionary words
for (int i = 0; i < dict.length; i++) {
// key and value are identical
dictionary.put(dict[i], dict[i]);
}
}
dictReader.close();
String user_text = "";
// Initializing a spelling suggestion object based on probability
SuggestSpelling suggest = new SuggestSpelling("wordprobabilityDatabase.txt");
// get user input for correction
while (!user_text.equalsIgnoreCase("q")) {
// CleanString is a string full of words to be spell checked
user_text = cleanString;
String[] words = user_text.split(" ");
int error = 0;
for (String word : words) {
suggestWord = true;
String outputWord = checkWord(word);
if (suggestWord) {
System.out.println("Suggestions for " + word +
" are: " + suggest.correct(outputWord) + "\n");
error++;
}
}
if (error == 0 & !user_text.equalsIgnoreCase("q")) {
System.out.println("No mistakes found");
}
}
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
You never ask the user if he/she wants to quit or not, from inside the while loop. 您永远不会从while循环内部询问用户是否要退出。 So user_text
is initialized with cleanString
, and never changes inside the loop, hence the infinite loop. 因此, user_text
是使用cleanString
初始化的,并且在循环内永不更改,因此为无限循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.