# Infinite Looping problem

• April 2nd, 2014, 06:00 PM
ekrocks
Infinite Looping problem
Its a basic program that is played by 2 people. Player one is suppose to type a number and the second player is suppose to guess the number. However after I test it out, and if I guess too low or too high I get stuck in "Your guess is too low, try again." infinite loop and I have no idea what is wrong.
Code java:

```import java.io.*;   class GuessingGame { public static void main (String[] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); String firstPlayer, secondPlayer; int firstInput, secondInput; int guessCount = 0;   System.out.println("Player One, please input any integer lower than 1000"); firstPlayer = stdin.readLine (); firstInput = Integer.parseInt (firstPlayer);   System.out.println("Player Two, please guess the number"); secondPlayer = stdin.readLine (); secondInput = Integer.parseInt (secondPlayer);   {   while (secondInput < firstInput) { System.out.println("Your guess was too low, try again."); guessCount ++; } }     { while (secondInput > firstInput) { System.out.println("Your guess was too high, try again."); guessCount ++;   } }       if (secondInput == firstInput) { System.out.println("You have guessed the correct number, the correct number was: " + firstInput); guessCount ++; System.out.println("Your total guess was: "+ guessCount);   }   }   }```

• April 2nd, 2014, 06:06 PM
Norm
Re: Infinite Looping problem
With most loops, if the condition to end the loop is never reached, the loop goes forever.
With the loop in the code it will continue until (secondInput < firstInput) is false. For that to happen the value of one or the other or both of those variables must change.

Why use a loop? Would an if work?
• April 2nd, 2014, 06:17 PM
GregBrannon
Re: Infinite Looping problem
Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.
• April 2nd, 2014, 06:21 PM
ekrocks
Re: Infinite Looping problem
but if I use "if" it doesn't ask back for another guess even though its wrong.
• April 2nd, 2014, 06:29 PM
Norm
Re: Infinite Looping problem
You need to consider where the loop should be and when to exit the loop.
Basically:
stay in the loop until the right answer or the maximum guesses have been made.
Exit the loop with the correct guess.
• April 2nd, 2014, 06:38 PM
ekrocks
Re: Infinite Looping problem
I am really sorry for being really hard to deal with, but I quite don't understand what you mean. I am really confused to whether once the guess is wrong, can it go back to the "guessing" line again or will it just keep being stuck at its "while" infinite loop?
• April 2nd, 2014, 06:48 PM
Norm
Re: Infinite Looping problem
The logic could be something like this:
init variables
begin loop
get response
if correct answer -> exit loop
end loop
• April 2nd, 2014, 06:55 PM
ekrocks
Re: Infinite Looping problem
Ok, so last question :) is there a command for end loop? or is is just use of squiggly brackets?
• April 2nd, 2014, 07:00 PM
Norm
Re: Infinite Looping problem
The end of the loop would be the } for a while loop.
• April 2nd, 2014, 07:02 PM
ekrocks
Re: Infinite Looping problem
ok, thanks a lot for helping me. I kinda get what I need to do now thanks to you :)
• April 2nd, 2014, 07:07 PM
Norm
Re: Infinite Looping problem
ok. Good luck. Come back if you have more problems.