2016-04-23 6 views
-1

私は仕事をすることができない割り当てをしています。アイデアは.txtファイルから "辞書"をツリーセットにロードし、ランダムな文字(6〜10文字)を生成し、それらの文字から単語を構成しようとします。プログラムは、推測された単語がツリーセットに含まれているかどうかをチェックします。しかし、推測が終わったら、CTRL + Zを入力すると、プログラムは終了し、辞書に存在するランダムな文字から構成可能なすべての単語を印刷します。いくつかの要件を満たすTreeSetからすべての文字列を出力しますか?

私の問題は、CTRL + Zと入力したときに、ほとんどの場合、すべての可能な推測可能な単語が印刷されないことがあります。

私は何が欠けていますか?ここで

は私のコードです:

public class AngloTrainer { 

TreeSet<String> dict = new TreeSet<String>(); 
int ranNum; 
String randomWord; 

public AngloTrainer(String dictionaryFile) throws IOException { 
    loadDictionary(dictionaryFile); 
    System.out.println(dict.size() + " words loaded from dictionary.txt "); 
    Random randNumb = new Random(); 
    ranNum = (randNumb.nextInt(6) + 4); 
    randomWord = randomLetters(ranNum); 
    System.out.println("The random letters are: " + randomWord); 
    Scanner reader = new Scanner(System.in); 
    System.out.println("Guess a word!"); 
    try{   
     while(reader.hasNextLine() != false){ 
      String gWord = reader.next(); 
      if(includes(sort(randomWord), sort(gWord))){ 
       if(dict.contains(gWord)){ 
        System.out.println("ok!"); 
       }else{ 
        System.out.println("not ok!"); 
       } 
      }else{ 
       System.out.println("not ok!"); 
      } 
     } 
    }finally{ 
     reader.close(); 
     printWords(); 
     System.out.println("bye"); 
     System.exit(0); 
    } 
} 

//print all the words that can be guessed 
public void printWords(){ 
    for(String words: dict){ 
     if(includes(randomWord, words)){ 
      System.out.println(words); 
     } 
    } 
} 

//sort the letters in a String alpabetically 
private String sort(String s){ 
    char[] charArray = s.toCharArray(); 
    Arrays.sort(charArray); 
    return new String(charArray); 
} 

//print out the whole dictionary 
private void dumpDict() { 
    for(String word: dict){ 
     System.out.println(word); 
    } 
} 

//load the words from .txt file into TreeSet 
private void loadDictionary(String fileName) throws IOException{ 
    BufferedReader bufRead = new BufferedReader(new FileReader(new File(fileName))); 
    for (String line; (line = bufRead.readLine()) != null;) { 
     dict.add(line); 
    } 
    bufRead.close(); 
} 

//generate random letters 
private String randomLetters(int length) { 
    Random randomGenerator = new Random(); 
    String letters = "aabcdeefghiijklmnoopqrstuuvwxyyz"; 
    StringBuffer buf = new StringBuffer(length); 
    for (int i = 0; i < length; i++) 
     buf.append(letters.charAt(randomGenerator.nextInt(letters.length()))); 
    return buf.toString(); 
} 

//check if one the letters in one string is included in the other one 
private boolean includes(String a, String b) { 
    if (b == null || b.length() == 0) 
     return true; 
    else if (a == null || a.length() == 0) 
     return false; 
    int i = 0, j = 0; 
    while (j < b.length()) { 
     if (i >= a.length() || b.charAt(j) < a.charAt(i)) 
      return false; 
     else if (b.charAt(j) == a.charAt(i)) { 
      i++; j++; 
     } else if (b.charAt(j) > a.charAt(i)) 
      i++; 
    } 
    return true; 
} 

public static void main(String[] args) throws IOException { 
    AngloTrainer at = new AngloTrainer("C:/Some/Where/In/Files/dictionary.txt"); 
} 

}

答えて

0

CTRL + Zが、これは、入力を終了するには特に良い方法ではありません、あなたのプログラムにSIGTSTPを送信します。入力を終了する代わりに、プロセスを中断します。 CTRL + Dを使用して、EOT(End Of Transmission)文字を入力します。これにより、入力が適切に終了し、プログラムが進行します。

+0

助けてくれてありがとう! –

関連する問題