0

So this program is supposed to be a game where a user enters a phrase and another user tries to guess the phrase. I'm having a problem with buying a vowel, however. No matter what I enter at line 41 (, it just goes past the loop. I made the validAnswer(String answer) method in the hopes that it would be useful in the loop but it doesn't look to be much help. I know there is a small thing I'm missing but I'm too new to Java to recognize it yet. What am I doing wrong?

I apologize for the long amount of code.

import java.util.Scanner;

public class PhraseGame {

public static void main(String[] args) {

    // Initialize scanner
    Scanner stdIn = new Scanner(System.in);

    // Initialize variables
    String sPhrase;
    String answer = "f";
    char cGuess = 0;
    char vGuess = 0;
    int count = 0;
    int vowels = 0;
    int consonants = 0;
    int spaces = 0;
    boolean gameOver = false;
    boolean correct = true;

    // Start the "fun" game
    System.out.print("Please enter the phrase to guess at: ");
    sPhrase = stdIn.nextLine();

    // Create the temporary Array
    char [] tmpArr = new char[sPhrase.length()];
    for (int i = 0; i < sPhrase.length(); i++) {
        tmpArr[i] = sPhrase.charAt(i);
    }

    // Gets the number of spaces
    spaces = initTemplateArray(sPhrase, tmpArr, spaces);
    printTemplateArray(tmpArr);

    while (!(endGame(gameOver, spaces, consonants, vowels, sPhrase))) {
        cGuess = getConsonant(stdIn, cGuess);

        do {
            System.out.print("\nWould you like to buy a vowel?: ");
            answer = stdIn.next();

            if (answer == "y") {
                getVowel(stdIn, vGuess); }

            else if (answer == "n"){
                break;
            }
        } while (!validAnswer(answer));


        // Updates the array and prints it
        updateTemplateArray(tmpArr, sPhrase, cGuess, vGuess, count, vowels, consonants);
        printTemplateArray(tmpArr);

        // Checks if the game is over
        endGame(gameOver, spaces, consonants, vowels, sPhrase);
    }

}


// returns the number of space characters used in the common phrase
public static int initTemplateArray(String sPhrase, char [] tmpArr, int spaces) {
    for (int i = 0; i < sPhrase.length(); i++) {
        if (tmpArr[i] != ' ') {
            tmpArr[i] = '?';
        } else {
            tmpArr[i] = ' ';
            spaces++;
        }
    }

    return spaces;
}



public static void printTemplateArray(char [] tmpArr) {
    System.out.println("\nCommon Phrase");
    System.out.println("-------------");
    for (int i = 0; i < tmpArr.length; i++) {
        System.out.print(tmpArr[i]);
    }
}

public static boolean isVowel(char c) {
    return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}

public static char getConsonant(Scanner stdIn, char cGuess) {
    do {
        System.out.println("\nEnter a lowercase consonant guess: ");
        cGuess = stdIn.next().charAt(0);
    } while (isVowel(cGuess));

    return cGuess;
}

public static char getVowel(Scanner stdIn, char vGuess) {
    do {
        System.out.println("\nEnter a lowercase vowel guess: ");
        vGuess = stdIn.next().charAt(0);
    } while (!(isVowel(vGuess)));

    return vGuess;
}

public static int updateTemplateArray(char [] tmpArr, String sPhrase, char cGuess, char vGuess, int count, int vowels, int consonants) {
    for (int i = 0; i < sPhrase.length(); i++) {
        if (cGuess == sPhrase.charAt(i)) {
            tmpArr[i] = sPhrase.charAt(i);
            count++;
            consonants++;
        }

        if (vGuess == sPhrase.charAt(i)) {
            tmpArr[i] = sPhrase.charAt(i);
            count++;
            vowels++;
        }
    }

    return count & vowels & consonants;
}

public static boolean endGame(boolean gameOver, int spaces, int consonants, int vowels, String sPhrase) {
    int total = spaces + consonants + vowels;

    if (total == sPhrase.length()) {
        return true;
    }

    else {
        return false;
    }
}

public static boolean validAnswer(String answer) {
    if (answer.equalsIgnoreCase("y")) {
        return true;
    }

    else if (answer.equalsIgnoreCase("n")) {
        return true;
    }

    else {
        return false;
    }

}

}

Chris
  • 45
  • 7

1 Answers1

0

You need to check for null.

public static boolean validAnswer(String answer) {
    if (answer!=null && (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("n"))) {
        return true;
    }
    return false;
}

or an other unconventional way.

public static boolean validAnswer(String answer) {
    if ("y".equalsIgnoreCase(answer) || "n".equalsIgnoreCase(answer))) {
        return true;
    }
    return false;
}

You need to fix

do {
    System.out.print("\nWould you like to buy a vowel?: ");
    answer = stdIn.nextLine();

    if ("y".equalsIgnoreCase(answer)) {
        getVowel(stdIn, vGuess); 
    } else if ("n".equalsIgnoreCase(answer)){
        break;
    }
} while (!validAnswer(answer));
StackFlowed
  • 6,664
  • 1
  • 29
  • 45