打印出TreeSet中符合某些要求的所有字符串?

时间:2016-04-23 12:05:43

标签: java algorithm data-structures

我正在做一项我似乎无法开展工作的任务。我们的想法是将.txt文件中的“字典”加载到树集中,并生成随机字母(6-10个字母)并尝试从这些字母构造单词。程序检查猜测的单词是否在树集中。但是当你完成猜测时,你可以输入CTRL + Z,程序将结束并打印出所有可能由字典中存在的随机字母组成的单词。

我的问题是当我输入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");
}

}

1 个答案:

答案 0 :(得分:0)

CTRL + Z向您的程序发送SIGTSTP,这不是结束输入的特别好方法。它暂停进程而不是结束输入。使用CTRL + D输入EOT(传输结束)字符,这将正确终止输入并让程序进行。

相关问题