简体   繁体   中英

Why aren't my words coming out less than 8 characters?

public String compWord() throws IOException, ClassNotFoundException
{
    // Local constants
    final int MAX_COUNT = 8;

    // Local variables
    BufferedReader reader = new BufferedReader(new FileReader("dictionary.txt"));       // Create a new BufferedReader, looking for dictionary.txt
    List<String> lines = new ArrayList<String>();                                       // New ArrayList to keep track of the lines
    String line;                                                                        // Current line
    Random rand = new Random();                                                         // New random object
    String word;                                                                        // The computer's word

    /********************* Start compWord *********************/

    // Start reading the txt file
    line = reader.readLine();

    // WHILE the line isn't null
    while(line != null)
    {
        // Add the line to lines list
        lines.add(line);

        // Go to the next line
        line = reader.readLine();
    }

    // Set the computers word to a random word in the list
    word = lines.get(rand.nextInt(lines.size()));

    if(word.length() > MAX_COUNT)
        compWord();

    // Return the computer's word
    return word;
}

From what I understand it should only be returning words less than 8 characters? Any idea what I am doing wrong? The if statement should recall compWord until the word is less than 8 characters. But for some reason I'm still get words from 10-15 chars.

Look at this code:

if(word.length() > MAX_COUNT)
    compWord();

return word;

If the word that is picked is longer than your limit, you're calling compWord recursively - but ignoring the return value, and just returning the "too long" word anyway.

Personally I would suggest that you avoid the recursion, and instead just use a do / while loop:

String word;
do
{
    word = lines.get(rand.nextInt(lines.size());
} while (word.length() > MAX_COUNT);
return word;

Alternatively, filter earlier while you read the lines:

while(line != null) {
    if (line.length <= MAX_COUNT) { 
        lines.add(line);
    }
    line = reader.readLine();
}
return lines.get(rand.nextInt(lines.size()));

That way you're only picking out of the valid lines to start with.

Note that using Files.readAllLines is a rather simpler way of reading all the lines from a text file, by the way - and currently you're not closing the file afterwards...

If the word is longer than 8 characters, you simply call your method again, continue, and nothing changes.

So:

  • You are getting all the words from the file,

  • Then getting a random word from the List, and putting it in the word String,

  • And if the word is is longer than 8 characters, the method runs again.

  • But , at the end, it will always return the word it picked first. The problem is that you just call the method recursively, and you do nothing with the return value. You are calling a method, and it will do something, and the caller method will continue, and in this case return your word . It does not matter if this method is recursive or not.

Instead, I would recommend you use a non-recursive solution, as Skeet recommended, or learn a bit about recursion and how to use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM