Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Infinite Looping problem

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Location
    New Brunswick, Canada
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Angry 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.
    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);
     
                         }
     
                  }
     
       }


    Please help!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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.

  4. #4
    Junior Member
    Join Date
    Apr 2014
    Location
    New Brunswick, Canada
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Infinite Looping problem

    but if I use "if" it doesn't ask back for another guess even though its wrong.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Apr 2014
    Location
    New Brunswick, Canada
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Infinite Looping problem

    The logic could be something like this:
    init variables
    begin loop
    ask question
    get response
    if correct answer -> exit loop
    print message describing wrong answer
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Apr 2014
    Location
    New Brunswick, Canada
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Infinite Looping problem

    Ok, so last question is there a command for end loop? or is is just use of squiggly brackets?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Infinite Looping problem

    The end of the loop would be the } for a while loop.
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    ekrocks (April 2nd, 2014)

  11. #10
    Junior Member
    Join Date
    Apr 2014
    Location
    New Brunswick, Canada
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Infinite Looping problem

    ok, thanks a lot for helping me. I kinda get what I need to do now thanks to you

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Infinite Looping problem

    ok. Good luck. Come back if you have more problems.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problem with Looping...
    By the newbie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2013, 02:29 PM
  2. Problem with looping
    By gajen in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 27th, 2013, 07:56 PM
  3. Infinite loop problem
    By jacobjc3 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 30th, 2012, 09:41 PM
  4. Looping method problem.
    By Swen in forum What's Wrong With My Code?
    Replies: 24
    Last Post: December 29th, 2011, 09:49 AM
  5. Looping / OutOfBounds code problem
    By thisbeme in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 3rd, 2011, 07:10 AM