0

I am doing the tutorial on Java EE7 that comes with glassFish installation. It is also available here. The code is present in glassFish server installation directory

/glassFish_installation/glassfish4/docs/javaee-tutorial/examples/cdi/guessnumber-cdi.

The code works fine as it is. It currently displays correct! when a user correctly guesses the number but does not display failed at end of the game. so I introduced, just one minor change to display the failed message. I have added comments right above the relevant change in code.

Somehow, this change did not help. That is, the at the end of the game, failed message is not displayed. But the game works as usual. I would like to know why this did not work and how to correct it?

Thanks

public class UserNumberBean implements Serializable {

    private static final long serialVersionUID = -7698506329160109476L;

private int number;
private Integer userNumber;
private int minimum;
private int remainingGuesses;
@Inject
@MaxNumber
private int maxNumber;
private int maximum;
@Inject
@Random
Instance<Integer> randomInt;

public UserNumberBean() {
}

public int getNumber() {
    return number;
}

public void setUserNumber(Integer user_number) {
    userNumber = user_number;
}

public Integer getUserNumber() {
    return userNumber;
}

public int getMaximum() {
    return (this.maximum);
}

public void setMaximum(int maximum) {
    this.maximum = maximum;
}

public int getMinimum() {
    return (this.minimum);
}

public void setMinimum(int minimum) {
    this.minimum = minimum;
}

public int getRemainingGuesses() {
    return remainingGuesses;
}

public String check() throws InterruptedException {
    if (userNumber > number) {
        maximum = userNumber - 1;
    }
    if (userNumber < number) {
        minimum = userNumber + 1;
    }
    if (userNumber == number) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage("Correct!"));
    }

    //if remainingGuesses is less than or equal to zero, display failed message
    //-----------------------------------------------
    if (remainingGuesses-- <= 0) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage("failed "));
    }
    return null;
}

@PostConstruct
public void reset() {
    this.minimum = 0;
    this.userNumber = 0;
    this.remainingGuesses = 10;
    this.maximum = maxNumber;
    this.number = randomInt.get();
}

public void validateNumberRange(FacesContext context,
        UIComponent toValidate,
        Object value) {
    int input = (Integer) value;

    if (input < minimum || input > maximum) {
        ((UIInput) toValidate).setValid(false);

        FacesMessage message = new FacesMessage("Invalid guess");
        context.addMessage(toValidate.getClientId(context), message);
    }
}
}
brain storm
  • 30,124
  • 69
  • 225
  • 393

1 Answers1

1

Adding the FacesMessage is actually working, the problem is that you are using postdecrement in your condition.

Postdecrement, as the name suggests, is decremented AFTER the execution of the statement containing the postdecrement.

That means, if you write:

if (remainingGuesses-- <= 0) {

the var remainingGuesses is decremented after the if-condition was evaluated.

In your case, when the last guess is checked, remainingGuesses is actually 1 and therefore the if-condition is not true and the message is not added.

Different obvious solutions:

    if (remainingGuesses-- <= 1) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage("failed "));
    }

or

    if (--remainingGuesses <= 0) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage("failed "));
    }

or

    remainingGuesses--;
    if (remainingGuesses <= 0) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage("failed "));
    }

See also:

Community
  • 1
  • 1
unwichtich
  • 13,712
  • 4
  • 53
  • 66
  • you got that! so stupid of me..but if the remaining guesses is 1, the game is not finished. so one more guess is available, which will be decremented in next iteration. what I mean is, this condition will be executed at some point. Just that you get one extra guesses than what you are supposed to get. right? – brain storm Oct 04 '14 at 05:35
  • No, the var is decremented after the condition is evaluated and there is no more guess available... – unwichtich Oct 04 '14 at 13:57