Carlton Jr. Carlton Jr. - 1 year ago 45
Java Question

TreeSet returns false when it should return true?

I want to check if a certain

is in my

It returns
when it should return
, 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 {
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.out.println("Guess a word!");
while(reader.hasNextLine() != false){
String gWord =;
if(includes(sort(randomWord), sort(gWord))){
System.out.println("not ok!");
System.out.println("not ok!");

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

private void dumpDict() {
for(String word: dict){

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

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))
//postcondition: j == b.length()
return true;

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

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 Source

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){

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