简体   繁体   中英

License game Java loop crash

In California every car license plate contains 3 letters. We have a game where the objective is to make a word of the letters. For a word to match a license plate, it has to contain all the letters in the license plate in the order in which they occur. KDN = kidding. YRW = eyebrows. You get it.

So I have made a code for this game and it works fine except that it does not loop back to the start after it has found all the words for an input. I would like it so that i do not have to exit the "run-window" and run it again, for every run of the program. I have tried to find the bug but I cannot seem to find where I have gone wrong. Help greatly appreciated! And also,any tips on how to make this program more compact/better?

Here is the code:

import acm.program.*;
import java.io.*;


public class LicensePlateGame extends ConsoleProgram {


    public void run(){
        println("This program is the blueprint for the license plate game!");
        while(true){
            String letter = readLine("Enter three letters: ");
            String letters = letter.toLowerCase();
            try {
                BufferedReader rd = new BufferedReader(new FileReader("dictionary.txt"));
                // Breaks the input string apart to 3 characters
                char one = letters.charAt(0);
                char two = letters.charAt(1);
                char three = letters.charAt(2);

                while(letters.length() <= 3){
                    String line = rd.readLine();
                // Finds the words that follow the game rules
                    if(line.indexOf(one) != -1 && line.indexOf(two) > line.indexOf(one)  && line.indexOf(three) > line.indexOf(two)){
                        println(line);
                    }       
                }
                println("Enter legit input");
            }
            catch (IOException x){
                println("Error occured");
            }
        }
    }
}

This is bad:

while(letters.length() <= 3){
    String line = rd.readLine();
    // Finds the words that follow the game rules
    if(line.indexOf(one) != -1 && line.indexOf(two) > line.indexOf(one)  && line.indexOf(three) > line.indexOf(two)){
         println(line);
    }       
}

A while statement where none of the components of the terminating condition is changed in the body of the loops spells disaster: it's an infinite loop.

What is the condition? Stop when all words in the dictionary have been processed!

String line;
while( (line = rd.readLine()) != null ){
    if( line.length() >= 3 ){
        if( line.indexOf... ){
            System.out.println( line );
        }
    }
}
rd.close();

Below are my observations and suggestions .

It seems that both of your loops are infinite loops

for second loop you have a condition in while which will never fail and also you do not break loop in any condition.

1st step - add break when you get the required word.

while(letters.length() <= 3){
                String line = rd.readLine();
            // Finds the words that follow the game rules
                if(line.indexOf(one) != -1 && line.indexOf(two) > line.indexOf(one)  && line.indexOf(three) > line.indexOf(two)){
                    println(line);
                break; // break when matching String found 
                }       
            }

But above loop still can be infinite if your dictionary doest have any word matching to your rule.

2nd Step - come out of loop when you read complete dictionary

   String line = null;
    while(null!=(line = rd.readLine())){  // break when complete file has been read
     if(line.indexOf(one) != -1 && line.indexOf(two) > line.indexOf(one)  && line.indexOf(three) > line.indexOf(two)){
                        println(line);
                        break; // break when matching word found   
                    }  
}

FirstLoop while(true){ will also execute infinite times and it is not a good practice . To terminate this only option left with you is to close this program .

Proposed Solution : You could ask user that do you want to continue game? read the input .. if input is yes then continue else you can come out of loop . pseudoCode can look like

 String continueGame = "Yes"
 while("Yes".equals(continueGame)){

  // put your logic 
       continueGame = readLine("you want to play game again, enter Yes or No");
 }

Note:-

It is very important to close your BufferedReader , more specifically close it in finally block so it would be closed an all conditions .

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