-2

I have a boolean Guess function:

public boolean guess() {
  String checkInput = scanner.nextLine();

  try {
    guess = Integer.parseInt(checkInput);
  } catch (NumberFormatException e) {
    return false;
  }

  return true;
}

which is called by another function in a do while loop:

while (!player.guess()) {
  player.guess();
}

If I enter an int, the program runs properly and terminates. But if input is a non-int character, the program gets stuck in the while loop. I don't know what's going on here.

user1803551
  • 12,965
  • 5
  • 47
  • 74
grammerPro
  • 195
  • 1
  • 1
  • 9
  • You got error because of this `Integer.parseInt`. You expect int. – Aleksandr Podkutin Apr 26 '17 at 16:53
  • what do you mean by stuck? you sure it's not just waiting for your next input? – Shiping Apr 26 '17 at 16:56
  • You are calling `guess()` twice in each iteration, once in condition check, another time in loop body. Something tells me that you are looking for: [How to use Scanner to accept only valid int as input](http://stackoverflow.com/questions/2912817/how-to-use-scanner-to-accept-only-valid-int-as-input) – Pshemo Apr 26 '17 at 16:58
  • If I input a non-int value, like g, and then I input a 7, the program will not exit. I have to input an int value twice for the program to exit the while loop, like 7, 7. – grammerPro Apr 26 '17 at 17:00
  • To clarify your question use [edit] option. Don't post crucial info in comment (don't make people willing to help you search for that info). – Pshemo Apr 26 '17 at 17:00
  • `while(!player.guess());` this will work. – Sanket Makani Apr 26 '17 at 17:01
  • @grammerPro, I have updated my solution. I hope it works for you now :) – Devendra Lattu Apr 26 '17 at 17:15

3 Answers3

0

Your scanner.nextLine() reads the line forever, it doesn't ask for another input.

Turtle
  • 1,626
  • 16
  • 26
0
while (!player.guess()) { // Entered Integer. (!true) and loop breaks
    player.guess();
}

while (!player.guess()) { // Entered Non-Integer. (!false) and program enters the loop
    player.guess();  // You are not storing the result in some boolean variable
    //Therefore, it doesn't matter whether true or false
    //while (!player.guess()) will be checked once again
}

SOLUTION:

    boolean flag = player.guess(); // capture the result
    while (!flag) { // verify the result
        flag = player.guess(); // capture the result again
    }
Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27
0

Your guess function is designed that way. It returns false if the input is not numeric (catch). So it stays in the loop until you input a numeric value. Another problem is that you are calling the function twice every loop (once on the loop condition check and another inside the loop). So if you type a non numeric character on the first (loop condition) and a numeric on the second (inside the loop) it will still ask for an input a third time.

I don't know what your intention is but you probably would want something like:

while (!player.guess()) {
  continue;
}

Unless you really want it to be called twice.

Guga
  • 81
  • 4
  • That makes sense. But I don't know how I go about coding that into a solution. – grammerPro Apr 26 '17 at 17:05
  • This solution fixed my problem. The program prompts only 1 more time for an int input and then exits. I didn't know about using this construct with 'continue' before. Thanks! – grammerPro Apr 26 '17 at 17:09