Java中的邪恶刽子手。获得NullPointerException

时间:2014-12-11 01:55:00

标签: java nullpointerexception hashmap

所以我正在编写一个在控制台中播放的EvilHangman.java游戏。我相信你们之前都听说过这种类型的实验室,但它基本上可以尝试所有尝试而不是让用户获胜。每当我输入猜测时,我都会收到空指针错误。它与arraylists有关,以方法wordsLeft()为中心。任何和所有的帮助都会很棒。我感到愚蠢,我无法弄明白,因为我在这个问题上花了最近几天。我对任务的处理方法可能不是解决问题的最有效方法,但这是我的实验室任务所需要的。有一个单词类可以上传字典中的所有英文单词。

while循环在我的主课程中完成游戏的所有工作:

while((numGuess != 0) ) {

        if(remaining == true )
            System.out.println("There are " + wordsLeft() + " possible words left.") ;
        System.out.println("You have guessed these letters [" + allLetters + "]. And this is what your word looks like: " + patternHandler() ) ;
        System.out.println("You have " + numGuess + " guess(es) left. Please enter a single letter guess: ") ;
        String temp = keyboard.nextLine() ;
        String guess = temp.toLowerCase() ;
        if(guess.length() > 1) {
            System.out.println("You entered too many characters. Enter a new guess. This did not use one of your guesses.") ;
            temp = keyboard.nextLine() ;
            guess = temp.toLowerCase() ;
        }
        if (alreadyGuessed(guess)){
            System.out.println("You have already guessed that letter, please guess again. This did not use one of your guesses.");
            temp = keyboard.nextLine() ;
            guess = temp.toLowerCase() ;
        }
        if(!alreadyGuessed(guess)) {
            allLetters += guess ; //keep track of all the letters that have already been guessed.
            numGuess--;
            count++ ;
        }
        currentList = Family.biggestList ;
        System.out.println(patternHandler());
    }

    if(solved(patternHandler()))
        System.out.println("You have won the game! Please play again!") ;
    else
        System.out.println("Sorry you have lost.. please try again!") ;

}//main

/**
 * Checks to see if a letter has already been guessed.
 * @param let the user's guess
 * @return True if the letter has been guessed before
 */
private static boolean alreadyGuessed(String let) {
    if(allLetters.length() == 0)
        return false ;
    for(int i = 0; i<=allLetters.length()-1 ; i++) {
        if (allLetters.charAt(i) == let.charAt(0))
            return true;
        }
    return false;
}//containsLetter

/**
 * Decides whether or not the user wants to see how many words are possibly left
 * @param temp user input, either yes or no.
 * @return true if the user inputs yes.
 */
private static boolean runningTotal(String temp) {      
    if (temp.equalsIgnoreCase("yes"))
        return true;
    else
        return false;       
}//runningTotal

/**
 * Checks to see if the user figured out the word.
 * @param wrd the current pattern of the possible words
 * @return true if the word does not contain '-'
 */
private static boolean solved(String wrd){
    for(int i = 0; i<wrd.length(); i++){
        if(wrd.charAt(i) == '-')
            return false ;
    }
    return true ;
}//solved
/**
 * Method to get the current word pattern.
 * @return a string representation of the current word pattern.
 */
private static String patternHandler() {
    String temp = "";
    for(int i = 0; i<length; i++){
        temp+= "-";
    }

    if(count == 0)
        return temp ;
    else
        return Family.maxKey ;
}
/**
 * Allows the user to know how many possible words are left.
 * @param list the current word list of possible end words
 * @return the size of list
 */
private static int wordsLeft() {
    if(count==0)
        return startList.size() ;
    else
        return Family.biggestList.size() ;
}

这部分在我的hashmap类中,它处理我想要使用的arraylist:

public static void main(String[] args) {

    int guessesLeft = numGuess ;
    int guessCount = numGuess ;
    initial = Words.sortedWords[length] ; 

    if(guessesLeft == guessCount) {
        getPatternForGuess(guess, initial) ;
        guessCount-- ;
    }
    else
        getPatternForGuess(guess, biggestList) ;

    //the key of the wanted list
    int maxLength = 0; 

    //longest list of patterns
    biggestList = new ArrayList<String>() ;

    for(ArrayList<String> entry : families.values()) {
        if(biggestList == null || entry.size() > biggestList.size())
            biggestList = entry ;
    }
    for(Entry<String, ArrayList<String>> e : families.entrySet()) {
        int len = e.getValue().size() ; //size of the arraylist being looked at
        if(maxKey == null || len > maxLength) {
            maxKey = e.getKey() ;
            maxLength = len ;
        }
    }


}//main

/**
 * This method returns a HashMap of possible patterns and the words they contain.
 * @param guess the user's guess obtained from EvilHangman
 * @param list the list of possible words
 * @return newMap a hashmap of all patterns and the ArrayList of words.
 */
public static HashMap<String, ArrayList<String>> getPatternForGuess(String guess, ArrayList<String> list) {

    HashMap<String, ArrayList<String>> newMap = new HashMap<String, ArrayList<String>>() ;
    String pattern = "" ;


    for(int i = 0; i<list.size(); i++) {
        String word = list.get(i) ;
        pattern = getPatternForWord(guess, word) ;

        if(!newMap.containsKey(pattern)) {
            ArrayList<String> wordList = new ArrayList<String>() ;
            wordList.add(word) ;
            newMap.put(pattern, wordList) ;
        }
        if(newMap.containsKey(pattern)) {
            ArrayList<String> wordList = new ArrayList<String>() ;
            wordList.add(word) ;
        }
    }
    return newMap ;
}//getPatternForGuess


//This method gets the pattern based on your guess and words like 
/**
 * Gets the pattern of the word based on the user guess
 * @param guess the user guess obtained from evil hangman
 * @param word the word pulled from the arraylist being mapped
 * @return word a string representation of the pattern.
 */
public static String getPatternForWord(String guess, String word) {
   char ch = guess.charAt(0) ;
   for(int i = 0; i<word.length(); i++) {
       if(word.charAt(i) != ch)
           word.replace(word.charAt(i), '-') ;
   }
   return word;
}//getPatternForWord

0 个答案:

没有答案