Results 1 to 8 of 8

Thread: Infinite loop

  1. #1
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default Infinite loop

    Hey there,

    still working on my first project, a simple dice game, as I mentioned already in another thread. So far everything worked fine the way I wanted it to work, but now I got stuck with an infinite loop I can't figure out.

    In a game initializing class ("NewGame.java") I have this method:

    Java Code:
    public static int newOpponents(String playersName) {
    		Scanner input = new Scanner(System.in);
    		int numberOfOpponents = 0;
    		
    		do {
    			System.out.println();
    			System.out.print("Hello " + playersName + ". How many people do you want to play with (max. 5)? ");
    			try {
    			 numberOfOpponents = input.nextInt();
    			} catch (InputMismatchException wrongInput) {
    				numberOfOpponents = 999;
    			}
    		} while ((numberOfOpponents < 1) || (numberOfOpponents > 5));
    		return numberOfOpponents;
    	}

    From main(), I call it via

    Java Code:
    numberOfOpponents = NewGame.newOpponents(player1.getPlayersName());

    It works fine as long as I type in numbers. But if I type in any letter, I get caught in an infinite loop that just executes the

    Java Code:
    System.out.print("Hello " + playersName + ". How many people do you want to play with (max. 5)? ");

    statement. It doesn't seem to reach the try/catch anymore. Any ideas why that is?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    It doesn't seem to reach the try/catch anymore
    How do you know?
    For debugging you need to add a println() statement in the catch block to see if it is executing.

  3. #3
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    Okay, I changed the condition to

    Java Code:
    while (!((numberOfOpponents >= 1) || (numberOfOpponents <= 5)));

    and now it works as I want it to. But still I don't get what the problem was. Why didn't the whole construction execute try/catch anymore after typing in a letter?

  4. #4
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    How do you know?
    For debugging you need to add a println() statement in the catch block to see if it is executing.
    Right. I'll try that.

  5. #5
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    ?
    if you enter a letter then numberofoponents is 999
    999> then 5 so it just asks it again

  6. #6
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    Okay, after typing in a letter, numberOfOpponents is correctly set to "999", and the try/catch statement seems to still get executed. But why is there an infinite loop? Try/catch doesn't take any user input anymore, it just keeps on printing the question and "999". I don't see the problem.

    @dinosoep - Yes, that's what he is supposed to do. But the problem is that the question output is printed over and over again without any chance to make a new input. If I type in a number higher than 5, everything works fine. If I type in a letter, which also results in a number higher than 5, it doesn't. But why? And why does it work after changing the condition as shown above?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Why didn't the whole construction execute try/catch anymore after typing in a letter?
    The catch block was being executed, but your code didn't tell you that is was and your incorrect conditions keep the loop going.


    I don't see the problem.
    You also must see that the data in the Scanner's buffer is not being removed. Every time you call nextInt() the bad data is still there and an exception is thrown. You need to remove the bad data by calling either next or nextLine depending on how you are allowing the user to enter data.

  8. #8
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    You also must see that the data in the Scanner's buffer is not being removed. Every time you call nextInt() the bad data is still there and an exception is thrown. You need to remove the bad data by calling either next or nextLine depending on how you are allowing the user to enter data.
    Yep, that actually was the problem. I didn't know that. Now it works perfectly fine. Thanks a lot! :-)

Similar Threads

  1. Infinite Loop
    By bosoxfan in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 01:34 AM
  2. Infinite Loop
    By rclausing in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 10:11 PM
  3. Infinite running loop problems
    By BigDummy in forum New To Java
    Replies: 5
    Last Post: 10-14-2009, 06:39 AM
  4. Infinite loop when reading text file
    By MartinMorrison in forum New To Java
    Replies: 2
    Last Post: 08-03-2009, 04:36 PM
  5. java recursion infinite loop
    By tony404 in forum Advanced Java
    Replies: 9
    Last Post: 10-03-2008, 01:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •