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

Thread: Infinite loop?

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

    Question Infinite loop?

    Hi guys,

    My code freezes in the console after a BookID is input, following the prompt "Enter the bookID of the book you wish to loan out".
    Can't spot where I've gone wrong.

    Thanks!

    public static void issueBook()
        {
            // choose userID to issue to
            Scanner uid = new Scanner(System.in);
            System.out.println("\nEnter the userID of the user you wish to issue a book to:");
     
            int userIDToIssue = uid.nextInt(); // get userID from user input
            System.out.println("UserID taken in by scanner: " + userIDToIssue);
     
            String userToIssueFirstName = "";
            String userToIssueSurname = "";
            String userFullName = userToIssueFirstName + " " + userToIssueSurname;
     
            // find user by ID
            ListIterator<User> findUserID = userStore.listIterator();
            ListIterator<Book> findBookID = bookStore.listIterator();
            boolean userFound = false;
            boolean bookFound = false;
     
            while (findUserID.hasNext() && userFound == false)
            {
                User u = findUserID.next();
     
                if (u.getUserID() == userIDToIssue)
                {
                    userFound = true;  // stops while loop
                    //code to run:
     
                    if (u.booksHeld>=3)
                    {
                        System.out.println("The user already has 3 books. They must first return a book to be able to take out another book");
     
                        // Test userFound assignment to true:
                        System.out.println(userFound);
     
                        issueBook();
                    } else
                    {
                        // issue the book
     
                        // variables:
                        userToIssueFirstName = u.userFirstName;
                        userToIssueSurname = u.userSurname;
     
                        Scanner bid = new Scanner(System.in);
                        System.out.println("\nEnter the bookID of the book you wish to loan out");
     
                        int bookIDToIssue = bid.nextInt();
     
                        // copy userBorrowing name to Book object
                        while (findBookID.hasNext() && bookFound == false)
                        {
                            Book b = findBookID.next();
     
                            if (b.getBookID() == bookIDToIssue)
                            {
                                bookFound = true;   // stops while loop
     
                                // code to run:
                                if (b.onLoan == true)
                                    {
                                        System.out.println("This book is currently out on loan. A message will be generated for the current borrowee to notify them that it has been requested by somebody else.");
     
                                        // TODO: export message to text file
                                    } else if (b.onLoan == false)
                                    {
                                        b.userBorrowingFirstName = userToIssueFirstName; // name of user borrowing the book is set
                                        b.userBorrowingSurname = userToIssueSurname;
     
                                        b.onLoan = true; // book is set to being on loan
                                    } 
                                } else
                                {
                                    System.out.println("Book does not exist");
                                    issueBook();
                                }
                            }
                            }
     
                            u.booksHeld ++; // then update the number of books held - end
                        }
                    }
                }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Infinite loop?

    Quote Originally Posted by jackfletcher
    ...Can't spot ...
    Try making the program tell you what it is seeing. Sometimes that helps...


    For example:
    1. Put a print statement after you get user input:
                          System.out.println("\nEnter the bookID of the book you wish to loan out");
       
                          int bookIDToIssue = bid.nextInt();
                          System.out.println("You entered bookIDToIssue = " + bookIDToIssue);

    2. Put a print statement inside the loop where you are comparing bookID values:
                          while (findBookID.hasNext() && bookFound == false) {
                              Book b = findBookID.next();
                              System.out.println("b.getBookID() = " + b.getBookID());
       
                              if (b.getBookID() == bookIDToIssue) {
      .

    3. Put a print statement after that while(){} loop:
                              else {
                                  System.out.println("Book does not exist");
                                  issueBook();
                              }
                          } // End of while (findBookID.hasNext() && bookFound == false)
                          System.out.println("After loop: bookFound = " + bookFound);

    4. Put additional print statements in logic branches where you think it should be going, but seems to be getting lost.



    Cheers!

    Z

    .
    Last edited by Zaphod_b; October 30th, 2012 at 01:09 PM.

Similar Threads

  1. [SOLVED] Infinite loop issue
    By Sharmeen in forum Loops & Control Statements
    Replies: 1
    Last Post: October 20th, 2012, 12:18 PM
  2. Infinite loop problem
    By jacobjc3 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 30th, 2012, 09:41 PM
  3. [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
  4. Confused about infinite loop
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: March 9th, 2011, 07:45 AM
  5. [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

Tags for this Thread