1

I am new to java and programming overall, so please bear with me. In the code below,when I run object.ValidateGuess(), I am propted 3 times "Please enter a letter to be guessed:". Why is that? Is it because I use the GetGuess() method in the if statement?? I want to use the return of that method only, not for it to run every time. thanks in advances

public char GetGuess(){
    System.out.println("Please enter a letter to be guessed");
    Scanner guess = new Scanner(System.in);
    return guess.nextLine().charAt(0);
}

public boolean ValidateGuess(){
    boolean isHit = false;
    int triesLeft = MAX_TRIES;
    if (mCorrectAnswer.indexOf(GetGuess()) < 0 ){
        System.out.println("Your guess was incorrect");
        triesLeft -= 1;
        System.out.println(triesLeft);
        mMisses += GetGuess();
    } else if (GetGuess() == (int)GetGuess()) {
        System.out.println("you have to input a letter!!!");
    } else {
        mHits += GetGuess();
        isHit = true;
    }
2
  • 2
    Then put the return value in a variable and have the if condition involve that variable rather than call the method itself. A method call is exactly that: it calls the method. Commented Aug 10, 2016 at 11:04
  • Because the method is executed on both if conditions plus on the else when the other conditions do not meet. Commented Aug 10, 2016 at 11:05

5 Answers 5

2

You call the method at multiple times,

if (mCorrectAnswer.indexOf(GetGuess()) < 0 ){
        [...]
        mMisses += GetGuess();
} else if (GetGuess() == (int)GetGuess()) {
        [...]
} else {
        mHits += GetGuess();
}

which means you are calling for a unique letter multiple times.

Remedy this by storing GetGuess() in a variable and readjusting:

char guess = GetGuess();
if (mCorrectAnswer.indexOf(guess) < 0 ){
        [...]
        mMisses += guess;
} else if (guess == (int)guess) {
        [...]
} else {
        mHits += guess;
}

If it helps, remember that every time you call a function, every single line is executed within that function every time regardless of how, where, when and why you are calling it. A method has one job, and that is to, literally, run through it's method.

Sign up to request clarification or add additional context in comments.

Comments

1

Exactly. Try storing the value, instead of calling the function again and again.

public boolean ValidateGuess(){
    boolean isHit = false;
    int triesLeft = MAX_TRIES;
    char guess = GetGuess();
    if (mCorrectAnswer.indexOf(guess) < 0 ){
        System.out.println("Your guess was incorrect");
        triesLeft -= 1;
        System.out.println(triesLeft);
        mMisses += guess;
    } else if (guess == (int)guess) {
        System.out.println("you have to input a letter!!!");
    } else {
        mHits += guess;
        isHit = true;
}

Comments

0

In your if conditions you call every time GetGuess so the user has to make an input again and again. You have to save the value from GetGuess and work with that.

(Btw: it is better style to write method names not in capital letters at the beginning).

You can change your code into that:

public char GetGuess(){
    System.out.println("Please enter a letter to be guessed");
    Scanner guess = new Scanner(System.in);
    return guess.nextLine().charAt(0);
}

public boolean ValidateGuess(){
    boolean isHit = false;
    int triesLeft = MAX_TRIES;
    char guessValue = GetGuess(); //Here you store the guess and now you can work with that in your if conditions
    if (mCorrectAnswer.indexOf(guessValue) < 0 ){
        System.out.println("Your guess was incorrect");
        triesLeft -= 1;
        System.out.println(triesLeft);
        mMisses += guessValue;
    } else if (guessValue == (int)guessValue) {
        System.out.println("you have to input a letter!!!");
    } else {
        mHits += guessValue;
        isHit = true;
    }

Comments

0

You probably should execute GetGuess() once and then store the result in a variable. In your following statements, you can then examine this variable:

char guessResult = GetGuess();
if (mCorrectAnswer.indexOf(guessResult) < 0){
    System.out.println("Your guess was incorrect");
    triesLeft -= 1;
    System.out.println(triesLeft);
    mMisses += guessResult;
} else if (guessResult == (int)guessResult) {
    System.out.println("you have to input a letter!!!");
} else {
    mHits += guessResult;
    isHit = true;
}

Comments

0

Yes, its because you are calling the method each time you put it in the if conditions. Try to store the return value of the method in a variable and use that variable instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.