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 4 of 4

Thread: Infinite loop problem

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Infinite loop problem

    Hello all, I'm working on a homework assignment involving recursion. The problem is I can't get to the recursion part because I've run into an IO problem that I can't figure out for the life of me. The whole point of the lab is doing Recursion for a binary search and I don't want help with that. I have run into a problem with getting the height/width of a rectangle from the user. If the user inputs a letter/character and not a number I get an infinite loop that is displayed as a result of catching an InputMismatchException. If anyone can give me some advice or point me in the right direction as how to fix this issue, I would greatly appreciate it.

    Below is the method causing the error.

    public static Rectangle getRectangle()
    {
    Scanner keyboard = new Scanner (System.in);

    Rectangle r = new Rectangle();

    System.out.print("Enter the height of the rectangle: ");
    try
    {
    r.height = keyboard.nextInt();
    }
    catch (InputMismatchException e)
    {
    System.out.println("You didn't enter a number. You must enter a positive integer.");
    }
    if (r.height <= 0)
    System.out.println("You must enter a positive integer.");
    System.out.print("Enter the width of the rectangle: ");
    r.width = keyboard.nextInt();

    return r;
    }


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Infinite loop problem

    I take it that the method getRectangle() is used inside of a loop, possibly in the main() method? Seeing the actual loop would probably help and I find it curious that you only catch the exception for r.height and not r.width. Another thing, Scanner.next() will make the scanner advance to the next input regardless of whether or not the input is correct. In other words if there is a InputMismatchException then the scanner will just revert to the last input and not ask for anymore input.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Infinite loop problem

    my apologies, I had taken out the loop before I posted the message. I didn't catch the exception for r.width because I wanted to get it right with r.height first and then just copy and paste it. I'm not quite sure what you mean by "scanner will just revert to the last input." This is the first thing that the user enters into the program. When a non-number character is entered it not only throws the error but also skips allowing the user to enter r.width.

  4. #4
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Infinite loop problem

    I suppose saying that it will revert wasn't quite right. If there is a mismatch then the Scanner won't advance to the next input and, in this case, keep using a non-integer value causing an infinite loop. You can manually skip the incorrect input by calling Scanner.next() which will let the user input something else (hopefully a correct value).

Similar Threads

  1. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  2. help me terminate the infinite loop.
    By ab7 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 10th, 2012, 03:40 AM
  3. Confused about infinite loop
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: March 9th, 2011, 07:45 AM
  4. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  5. Doubling hashing, infinite loop?
    By vluong in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 8th, 2009, 01:26 AM