Question
Here's the class for Hangman logic. I need help in implementing an error handling for the keyPhrase (make sure there are no special characters) that is being used in the constructor. I'm thinking it should be a try/catch exception error handling but I'm not so I hope someone can help. Here's what I have: import java.util.ArrayList; public class HangmanLogic { //setting variables and setting to private only accessable to this class private String keyPhrase; private int numberOfGuesses; private int numberOfGuessesLeft; private ArrayList guessedCharacters; //HangmanLogic Constructor public HangmanLogic(String KeyPhrase, int numberOfGuesses) { this.keyPhrase = keyPhrase; this.numberOfGuesses = numberOfGuesses; this.numberOfGuesses = numberOfGuesses; guessedCharacters = new ArrayList(); } //Accessor method: returns the word or phrase being guessed public String getKeyPhrase() { return keyPhrase; } /** * method returns the keyPhrase field in an encoded manner with dashes representing the still unknown letters and letters for the known letters * @return returns the encoded keyPhrase of this object (letters known and dashes) */ public String getKnownKeyPhrase() { char[] encodedKeyPhrase = new char[keyPhrase.length()]; String lowerCaseKeyPhrase = keyPhrase.toLowerCase(); for(int i = 0; i < keyPhrase.length(); i++) { char cur = lowerCaseKeyPhrase.charAt(i); if(guessedCharacters.contains(cur)) encodedKeyPhrase[i] = cur; else encodedKeyPhrase[i] = '-'; } return (new String(encodedKeyPhrase) ); } //Accessor method:returns the numberOfGuesses field of this object public int getNumberOfGuesses() { return numberOfGuesses; } //Accessor method: returns the numberOfGuessesLeft field of this object public int getNumGuessesLeft() { return numberOfGuessesLeft; } /** * Throws InvalidInputException: if a non-valid character is passed as input to this method * ThrowsAlreadyGuessedException: if a valid character which has already been guessed is passed * as input to this method * Method returns if a character exists in the key phrase. * Character is valid then method return true. If not, it is false. */ public boolean guessCharacter(char guess) throws InvalidInputException, AlreadyGuessedException { if(isValidCharacter(guess)) { guess = Character.toLowerCase(guess); if(guessedCharacters.contains(guess) ) { throw new AlreadyGuessedException("" + guess); } else { guessedCharacters.add(guess); if(keyPhrase.contains("" + guess)) return true; else { numberOfGuessesLeft--; return false; } } } else { throw new InvalidInputException("" + guess); } } private boolean isValidCharacter(char guess) { // TODO Auto-generated method stub return false; } /** * Returns whether the guessing user is out of guesses in this game of hangman * Returns whether the guessing user is out of guesses in this game of hangman */ public boolean isGameOver() { boolean guessed = (getKeyPhrase().equals(keyPhrase.toLowerCase())); return( numberOfGuessesLeft == 0 || guessed); } }
Explanation / Answer
import java.io.File; import java.io.IOException; import java.util.Scanner; public class scanWordsFromFile { public static void main(String args[]) throws IOException { //This creates a new Scanner object that reads the chose file. Scanner scan= new Scanner(new File("C:\.hangman\hangman.txt")); String words[]; int lines; //These loops assign all the words in the document to a String array. //The first loops counts how many lines there are. for (lines=0; scan.hasNextLine(); lines++) { scan.nextLine(); } words= new String[lines]; //Reset scan scan= null; scan= new Scanner(new File("C:\.hangman\hangman.txt")); //The second loop assigns values the String array "words". for (int loop=0; loop