So I spent some time studying up on util.Scanner thanks to some feedback here. The statement that stuck with me the most was found in this post on How to use java.util.Scanner to correctly...
You will probably never actually see Scanner used in professional/commercial line of business apps because everything it does is done better by something else. Real world software has to be more resilient and maintainable than Scanner allows you to write code. Real world software uses standardized file format parsers and documented file formats, not the adhoc input formats that you are given in stand alone assignments.
So, I looked up some other options for getting user input and decided to use BufferedReader. What I'm trying to accomplish is to get the user to tell the program whether they would like to continue playing the game or not, to have the program repeat the question until given valid input (Y/N), and to have the program continue or terminate as a result of the answer.
Three related questions:
Is BufferedReader a better option than Scanner?
Is there an option that better addresses this task? What is it, if so?
Is it appropriate to have the method call itself to create a "loop" if the answer does not fit the criteria?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RageGame {
public static void main(String[] args) throws IOException {
char playAgain = 'Y';
// Loop game until user decides they are done.
do {
playAgain = playRage();
} while (playAgain == 'Y');
//Quit message
System.out.println("Thanks for playing Rage!");
}
public static char playRage() throws IOException {
return playAgain();
}
public static char playAgain() throws IOException{
char answer;
BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
//Ask the user if they want to continue and set their response to upper case.
System.out.println("Do you wish to play again? (y/n)");
answer = (char) Character.toUpperCase(kb.read());
//Check if answer fits criteria
if (answer != 'Y' && answer != 'N') {
System.out.println("Sorry, " + answer + " is not a valid answer.");
return playAgain();
}else {
return answer;
}
}
}