2017-04-01 53 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,並且在循環內部永遠不會改變,因此是無限循環。