java - TreeSet 在应该返回 true 时返回 false?

标签 java algorithm data-structures treeset

我想检查某个 String 是否在我的 TreeSet 中。

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

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:添加了我所有的代码。

最佳答案

来自评论:

loadDictionary() works as it should

然而,这是完全不正确的。现在我们可以看到它了,我们可以告诉您它只加载每隔一个单词。

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

while 循环中的readLine() 读取第一行。 add() 调用中的 readLine() 读取第二行并将其添加到 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);
    }
}

关于java - TreeSet 在应该返回 true 时返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36802069/

相关文章:

c++ - 如何设计一个最近最近使用的缓存?

c - 将结构与零进行比较的首选方法

Java - 查看列表模块是否包含某个值

Java、端口、套接字、通过程序管道连接

java - Raspberry Pi 2 - 在纵向模式下移动物体时出现失真

algorithm - 给一个文本 block ,一些快捷键和目标文本,如何获得最少的按键来转换文本?

linux - Bash 脚本在一个文件中查找单词,但在另一个文件中找不到。给定两个文件

algorithm - 是否存在可以解决 Vim Golf 问题的算法

java - 在LinkedList中使用long类型id

go - 在 Go 语言中返回其结构地址的方法