Carlton Jr. Carlton Jr. - 7 months ago 8
Java Question

TreeSet returns false when it should return true?

I want to check if a certain

String
is in my
TreeSet
.

It returns
false
when it should return
true
, but I'm not sure where i messed up in my code. Here is the code:

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


the
include()
method works fine, it compares two strings to see if the letters in one of them is included in the other one.

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


But when entering the words "car", "patrol" and "rat" in the code above, it returns "false" from the
dict.contains(word)


And all the three words above, is in my .txt file.

Do you have any ideas what went wrong? If you need more of my code, I'll edit it in, just let me know please.

EDIT: Sometimes when I try to guess some words, it return true, but most of the time it returns false (dict.contains()).

EDIT2: Added all my code.

Answer

From comment:

loadDictionary() works as it should

However that is entirely untrue. Now that we can see it, we can tell you that it only loads every other word.

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

The readLine() in the while loop reads the first line. The readLine() in the add() call reads the second line and adds it to dict.

The first line is discarded.

This repeats such that only even lines are added to dict.

Change the code to remember the line read by the loop.
Also, remember to close the file, e.g. by using try-with-resources.

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