簡體   English   中英

TreeSet在應返回true時返回false?

[英]TreeSet returns false when it should return true?

我想檢查某個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()可以正常工作

但是,這是完全不正確的。 現在我們可以看到它了,我們可以告訴您它只會加載其他所有單詞。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM