如果TreeSet返回true,则返回false?

时间:2016-04-22 19:41:28

标签: java algorithm data-structures treeset

我想检查String中是否有某个TreeSet

当它返回false时会返回true,但我不确定我在代码中搞砸了哪里。这是代码:

HashSet<String> dict = new HashSet<String>();
//TreeSet<String> dict = new TreeSet<String>(dicty);  //snabbare såhär?
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);
    randomWord = "carpatrol";
    System.out.println("The random letters are: " + randomWord);
    Scanner reader = new Scanner(System.in);
    System.out.println("Guess a word!");
    //System.out.println(dict.contains("car"));
    //System.out.println(dict.contains("patrol"));
    //System.out.println(dict.contains("rat"));
    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!");
        }
    }
    //reader.close();
}

private String sort(String s){
    char[] charArray = s.toCharArray();
    Arrays.sort(charArray);
    return new String(charArray);
}

private void dumpDict() {
    for(String word: dict){
        System.out.println(word);
    }
}

private void loadDictionary( String fileName ) throws IOException{
    BufferedReader bufRead = new BufferedReader(new FileReader(new File(fileName)));
    while(bufRead.readLine() != null){
        dict.add(bufRead.readLine());
    }
    //bufRead.close();
}

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();
}

private boolean includes( String a, String b ) {
    if ( b == null || b.length() == 0 )
        return true;
    else if ( a == null || a.length() == 0 )
        return false;   
    //precondition: a.length() > 0 && b.length() > 0
    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++;
    }
    //postcondition: j == b.length()
    return true;
}

include()方法工作正常,它会比较两个字符串,看看其中一个字母是否包含在另一个字符串中。

include("car");    //returns true
include("patrol"); //returns true
include("rat");    //returns true

但是当在上面的代码中输入“car”,“patrol”和“rat”时,它会从dict.contains(word)

返回“false”

上面的三个单词都在我的.txt文件中。

你有什么想法吗?如果您需要更多我的代码,我会对其进行编辑,请告诉我。

编辑:有时当我尝试猜测某些单词时,它会返回true,但大多数时候它会返回false(dict.contains())。

EDIT2:添加了我的所有代码。

1 个答案:

答案 0 :(得分:1)

来自评论:

  

loadDictionary()按原样运行

然而,这完全是不真实的。现在我们可以看到它,我们可以告诉你它只加载其他所有单词。

BufferedReader bufRead = new BufferedReader(new FileReader(new File(fileName)));
while(bufRead.readLine() != null){
    dict.add(bufRead.readLine());
}

readLine()循环中的while读取第一行。 readLine()调用中的add()会读取第二行并将其添加到dict

第一行被丢弃。

这样重复,只有偶数行添加到dict

将代码更改为记住循环读取的行 另外,请记得关闭文件,例如通过使用try-with-resources。

try (BufferedReader bufRead = new BufferedReader(new FileReader(new File(fileName)))) {
    for (String line; (line = bufRead.readLine()) != null; ) {
        dict.add(line);
    }
}