2017-04-01 46 views
0

我的拼写检查程序没有提供任何错误代码,只是拼写检查相同的单词无限重复。 有没有办法阻止这个无限循环,并且限制它检查的字数,例如,当十个不正确的单词被纠正时结束代码?为什么我的程序无限循环?

我几乎可以肯定的是,无限循环在这里这种方法的结果:

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); 
     } 
    } 
+0

'cleanString'的值是什么? –

+0

把一些系统日志和调试值。 –

+0

欢迎来到堆栈溢出!它看起来像你需要学习使用调试器。请帮助一些[互补调试技术](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之后仍然遇到问题,请随时返回一个[最小,完整且可验证的示例](http://stackoverflow.com/help/mcve),以说明您的问题。 –

回答

0

,如果他/她想退出与否,从while循环中你永远不会询问用户。所以user_text初始化为cleanString,并且在循环内部永远不会改变,因此是无限循环。