I'm taking an online java course using the acm package, so the following syntax may not be "orthodox java". At any rate, hopefully, the point I'm making here comes through to those familiar with the package and otherwise:
The code I have below works for validating user input for a game of hangman in java, but going in, I was sure that I could do this with simply one if else statement, but ultimately for expediency's sake, I settled for adding a nested if, else statement. It's really bugging me that I can't figure out how to refactor this code in one if else statement as I'm positive that there is at least one very obvious way to do this. Any help would be much appreciated!
private void turn()
{
println("The word now looks like this: " + currentWord);
println("You have " + guessesLeft + " guesses left.");
currentGuess = readLine("Your guess: ");
String temp = currentWord;
/*test for legality of guess
* 1) is it exactly 1 char long?(ie not blank nor a long string
* 2) is it a letter?
*/
if(currentGuess.length() == 0)
{
println("That is an illegal guess.");
}
else{
char guessedLetter = Character.toUpperCase(currentGuess.charAt(0));
if(currentGuess.length() != 1 || !Character.isLetter(guessedLetter))
{
println("That is an illegal guess.");
}
else{
/*scan through secret word to find a match between
* entered guess and secret word
*/ ......}
private String currentGuess;
private String currentWord;