Skip to content

Instantly share code, notes, and snippets.

@jtara1
Last active October 26, 2016 19:51
Show Gist options
  • Save jtara1/a522473fdea2b9766b90bd3ad5a641f5 to your computer and use it in GitHub Desktop.
Save jtara1/a522473fdea2b9766b90bd3ad5a641f5 to your computer and use it in GitHub Desktop.
/**
* Remove '\n' and '\r' characters from string
*/
private String removeCRLF(String string) {
return string.replace("\n", "").replace("\r", "");
}
/**
* Prints all possible guns the player can pick, gets user input, calls toLowerCase method
* on user input, and returns it if it matches that of a valid gun.
* This function loops until a valid gun name is entered.
* @return Only returns the name of the gun (all lowercase) if it's a valid name
*/
public String getGunChoice() {
String userInput = "";
String[] options = {"pistol", "rifle", "shotgun"};
while (userInput != (options[0]).toLowerCase() &&
userInput != options[1].toLowerCase() &&
userInput != options[2].toLowerCase()) {
System.out.printf(
"Enter the name of the gun you'd like to use.\n" +
"Options: %s, %s, or %s\n",
options[0], options[1], options[2]);
userInput = removeCRLF(keyboard.nextLine().toLowerCase().trim());
System.out.println(userInput); // debug
}
return userInput;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment