2016-04-22 15 views
0

特定のStringが私のTreeSetにあるかどうかチェックしたいと思います。TreeSetはtrueを返すべきときにfalseを返しますか?

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()方法が正常に動作し、それはそれらのいずれかで、文字が他の1に含まれているかどうかを確認するために2つの文字列を比較します。

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

しかし、言葉「車」、「パトロール」と上記のコードで「ネズミ」を入力するとき、それはdict.contains(word)

から「偽」を返し、上記の3つのすべての単語、私の中にあります.txtファイル。

何が問題になったのですか?私のコードをもっと必要とするなら、私はそれを編集します、ちょうど私に教えてください。

編集:時には私がいくつかの単語を推測しようとすると、それは真実を返しますが、ほとんどの場合、偽を返します(dict.contains())。

EDIT2:すべてのコードを追加しました。コメントから

+0

falseを返すのはどちらですか? 'include'または現在のコード? – Dadani

+4

ガイドラインとして、エラーを再現するために必要なコードの最小量を記入してください。 – flakes

+0

また、私は好奇心が強いです。 'sort'はここで何をしているのですか? – flakes

答えて

1

:それは完全に真実ではありませんが

loadDictionaryは()

それが必要として動作します。今、私たちはそれを見ることができます、それは他のすべての言葉だけを読み込むことができます。 whileループ内

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

readLine()最初の行を読み取ります。 add()readLine()は、2番目の行を読み取り、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); 
    } 
} 
+0

ああ、私はAndreasの助けをありがとう、ありがとう! –

関連する問題