Infinite loop: where is the problem?

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1310 Views - Last Post: 07 December 2011 - 06:06 PM Rate Topic: -----

#1 greenpool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 27-November 11

Infinite loop: where is the problem?

Posted 06 December 2011 - 08:51 PM

Hi there,

got another problem with the library program i'm trying to code.

it seems like its going in to an infinite loop and i can't figure out for the life of me why. i've taken this on as a pet project to learn java and its doing my head in now lol.

hopefully somebody can help me out here...


here's the logic in my program:

database = arraylist = list of people books have been lent to

while (true)

1. get name if empty
2. if database is emtpty, add person to database and proceed to lend book

[iterate through list of people in the database]

3. check if person already in the database

4. if the name is equal to the name in database then lend book (at this stage assuming name's are unique)
4.1 check if another book needs to be lent to same person
4.2 if yes: checkout book
4.3 if no: set username to empty

5. if name is not in the database
5.1 add person


now after adding person it should go back to the start of the while loop to kick off the cycle again. which its not doing :s

code:

	if (option.equals("2")){
			
			ArrayList<Person> getPeople = ml.getPeople();
			ArrayList<Book> getAvailableBooks = ml.getAvailableBooks();
			boolean checkoutBook = true;
			boolean contd = true;
			Person p1 = new Person();
			String username="";
			
			
				
			
			while(contd){
				
				//get name
				if (username.equals("")){
				System.out.println("Name of person you would like to lend the book to: ");
				 username=keyboard.nextLine();
				}
				
				
				if( getPeople.size() == 0){
					
					System.out.println("Databse is empty. Will add to Database..");
					
					p1.setName(username);
					getPeople.add(p1);
					System.out.println("Done.");
					
					
				}
				
				
				
				
				
							
				//iterate through list of available people
				for(int i = 0; i < getPeople.size(); i++) {
				   Person aperson = getPeople.get(i);
					
					
					
					//if there is a match
				  if (aperson.getName().equals(username)){
						
						p1 = aperson;
						
						//book(s) needs to be checked out
						while (checkoutBook == true){
							
							//print available books to lend
							for (Book book : getAvailableBooks) {
								
								System.out.println(book.title + " by "
										+ book.author);
								
							}//end-get available books
							
							
							System.out.println("Which book would you like to check out?");
							String bookName = keyboard.nextLine();
							
							
							//check if book name entered is in the database
							//if it is check it out - either successful or failed
							
							for (Book book : getAvailableBooks) {
								
								
								
								
								if (book.title.equalsIgnoreCase(bookName)){
									
									boolean result =ml.checkOut(book, p1);
									if(result==true){
										
										System.out.println("Check out: successful.");
										break;
									}
									else{
										System.out.println("Check out failed: Book has already been checked out or "
														+ p1.getName()
														+ " has reached Maximum book limit");
										
										break;
									}
							}//end-if: (book.title.equals("bookName"))
								
								
								
								
								else if (!book.title.equalsIgnoreCase(bookName)){
									
									System.out.println("Book doesn't exist");
									
								}
						}//end-for-get available books
							
							
							System.out.print("Would you like to check out another book to "+ p1.getName() + " [y/n]?");
							String choice = keyboard.nextLine();
							
							if (choice.equals("y")){
								
								checkoutBook = true;
								
							}
							else if (choice.equals("n")){
								
								checkoutBook = false;
								username="";
								
							}
							
						}//end-while-checkoutBook == true)
						
						
					}//end-if-aperson.getName().equals(username)
					
					
				  //if person doesn't exist already, add them.
				  else if (!aperson.getName().equals(username)){
					
						 System.out.println("Person doesn't exist. Will add to Database..");
						 
						 p1.setName(username);
						 getPeople.add(p1);
						 System.out.println("Done.");
					
					 }//end-!aperson.getName().equals(username)
				
					 
				System.out.println("exiting available people loop" + username);
					 
				}//end-available people
					
				
				
				System.out.println("in while contd loop" + username);
				
				
				
				
			}//end-while-contd
			
			
			
			
		}//end-if-option




output:
[code]
What would you like to do? 

1. Add Books 
2. Lend a book to a person 
3. Check which Books are available 
4. Exit 
Enter choice [1 | 2 | 3 | 4]: 1
Enter name of Book: War
Enter name of the Author for War:DD
Would you like to add another book? [y/n]
n

What would you like to do? 

1. Add Books 
2. Lend a book to a person 
3. Check which Books are available 
4. Exit 
Enter choice [1 | 2 | 3 | 4]: 2
Name of person you would like to lend the book to: 
Roby
Databse is empty. Will add to Database..
Done.
War by DD
Which book would you like to check out?
War
Check out: successful.
Would you like to check out another book to Roby [y/n]?n
Name of person you would like to lend the book to: 
Sue
Person doesn't exist. Will add to Database..
Done




Would somebody able to tell me whats causing this?

thanks a lot!

Is This A Good Question/Topic? 0
  • +

Replies To: Infinite loop: where is the problem?

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: Infinite loop: where is the problem?

Posted 06 December 2011 - 08:57 PM

Refactor it. Break the bits out into methods that do the work, instead of one long method.

While you're doing that, you'll find the problem and fix it. If you don't, it'll be easier to find the problem.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Infinite loop: where is the problem?

Posted 06 December 2011 - 08:59 PM

How do you think somebody can figure out which break works which for/while with such an horribly indented code ?

Indent your code correctly, and perhaps you'll find the error yourself by doing that exercise :)
Was This Post Helpful? 1
  • +
  • -

#4 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Infinite loop: where is the problem?

Posted 06 December 2011 - 08:59 PM

Yes, Jon's suggestion will help you a lot, but for now my immediate glance at your code showed me that you never set contd to false, so while(contd) is basically while(true).
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Infinite loop: where is the problem?

Posted 06 December 2011 - 09:03 PM

View PostSheph, on 06 December 2011 - 11:59 PM, said:

Yes, Jon's suggestion will help you a lot, but for now my immediate glance at your code showed me that you never set contd to false, so while(contd) is basically while(true).

Yes but there are break statements :) To which loop they apply ? That's another story
Was This Post Helpful? 1
  • +
  • -

#6 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Infinite loop: where is the problem?

Posted 06 December 2011 - 09:05 PM

Oh yeah. *passes blame to someone else like a college professor* "...So that's why you always properly indent your code, folks."
Was This Post Helpful? 1
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: Infinite loop: where is the problem?

Posted 06 December 2011 - 09:09 PM

View Postpbl, on 06 December 2011 - 11:03 PM, said:

View PostSheph, on 06 December 2011 - 11:59 PM, said:

Yes, Jon's suggestion will help you a lot, but for now my immediate glance at your code showed me that you never set contd to false, so while(contd) is basically while(true).

Yes but there are break statements :) To which loop they apply ? That's another story


Exactly. And frankly, I can't even figure out what exactly the problem is meant to be. Is it that we're in a loop, but nothing's happening, or that we get out of the loop and the program ends?


Whatever the case, the only thing that'll fix this refactoring.


@sheph: :)

This post has been edited by jon.kiparsky: 06 December 2011 - 09:10 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Infinite loop: where is the problem?

Posted 06 December 2011 - 09:16 PM

You have to be masochist to have a loop spreading over 150 lines of code

Follow jon.kiparsky's suggestion an refactor !

A loop should be visible on a single screen of your editor/IDE. Unless you have a screen that displays 150 lines of code you are on the bad track.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Infinite loop: where is the problem?

Posted 06 December 2011 - 09:28 PM

Poor greenpool you are kind of blasted here :)

Write at least a separate method for all your cases
Was This Post Helpful? 0
  • +
  • -

#10 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Infinite loop: where is the problem?

Posted 06 December 2011 - 09:32 PM

Ah, what a good laugh reading this thread. For future reference though, size 4 font in eclipse gets you 159 lines of code. On a 1080x1920 screen ofcourse

This post has been edited by Mylo: 06 December 2011 - 09:35 PM

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Infinite loop: where is the problem?

Posted 06 December 2011 - 09:37 PM

Lol)))

If you really use Eclipse:
<CTRL>a
<CTRL>i

will automatically correctly indent all the code for you :)
Was This Post Helpful? 0
  • +
  • -

#12 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Infinite loop: where is the problem?

Posted 06 December 2011 - 09:42 PM

There is also ctrl + shift + f which would be easier. :^:
Was This Post Helpful? 2
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Infinite loop: where is the problem?

Posted 06 December 2011 - 09:44 PM

Here is part of the job
There is no break to exit the while() loop.. so infinite loop
	public static void main( String[] args )  {

		if (option.equals("2")){

			ArrayList<Person> getPeople = ml.getPeople();
			ArrayList<Book> getAvailableBooks = ml.getAvailableBooks();
			boolean checkoutBook = true;
			boolean contd = true;
			Person p1 = new Person();
			String username="";

			while(contd){

				//get name
				if (username.equals("")){
					System.out.println("Name of person you would like to lend the book to: ");
					username=keyboard.nextLine();
				}

				if( getPeople.size() == 0){

					System.out.println("Databse is empty. Will add to Database..");

					p1.setName(username);
					getPeople.add(p1);
					System.out.println("Done.");
				}
				//iterate through list of available people
				for(int i = 0; i < getPeople.size(); i++) {
					Person aperson = getPeople.get(i);
					//if there is a match
					if (aperson.getName().equals(username)){

						p1 = aperson;

						//book(s) needs to be checked out
						while (checkoutBook == true){

							//print available books to lend
							for (Book book : getAvailableBooks) {

								System.out.println(book.title + " by "
										+ book.author);

							}//end-get available books


							System.out.println("Which book would you like to check out?");
							String bookName = keyboard.nextLine();


							//check if book name entered is in the database
							//if it is check it out - either successful or failed

							for (Book book : getAvailableBooks) {

								if (book.title.equalsIgnoreCase(bookName)){

									boolean result =ml.checkOut(book, p1);
									if(result==true){

										System.out.println("Check out: successful.");
										break;
									}
									else{
										System.out.println("Check out failed: Book has already been checked out or "
												+ p1.getName()
												+ " has reached Maximum book limit");

										break;
									}
								}//end-if: (book.title.equals("bookName"))


								else if (!book.title.equalsIgnoreCase(bookName)){

									System.out.println("Book doesn't exist");

								}
							}//end-for-get available books


							System.out.print("Would you like to check out another book to "+ p1.getName() + " [y/n]?");
							String choice = keyboard.nextLine();

							if (choice.equals("y")){
								checkoutBook = true;
							}
							else if (choice.equals("n")){
								checkoutBook = false;
								username="";
							}

						}//end-while-checkoutBook == true)
					}//end-if-aperson.getName().equals(username)
					//if person doesn't exist already, add them.
					else if (!aperson.getName().equals(username)){

						System.out.println("Person doesn't exist. Will add to Database..");

						p1.setName(username);
						getPeople.add(p1);
						System.out.println("Done.");

					}//end-!aperson.getName().equals(username)


					System.out.println("exiting available people loop" + username);

				}//end-available people
				System.out.println("in while contd loop" + username);
			}//end-while-contd
		}//end-if-option
	}




View PostMylo, on 07 December 2011 - 12:42 AM, said:

There is also ctrl + shift + f which would be easier. :^:

Thanks, I am using Eclipse since 10 years and didn't know that one :^:
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: Infinite loop: where is the problem?

Posted 06 December 2011 - 09:46 PM

I think he meant to stay in the loop. The problem seems to be that it doesn't repeat (if you read the original post)
So the problem, I suspect, is that somewhere in there he sets him up to loop through a bunch of false conditions, which means he goes into a hang.

Or it might be something else, but I'm not going looking for it.
Was This Post Helpful? 0
  • +
  • -

#15 greenpool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 27-November 11

Re: Infinite loop: where is the problem?

Posted 06 December 2011 - 11:00 PM

lol thanks guys. i'll do the refactoring & indenting n see if i can get it to work.

i guess the reason its so messy/long is cos it started really small and i got carried away with adding more functionality to the point the only goal was to get it working lol


View Postjon.kiparsky, on 06 December 2011 - 09:46 PM, said:

I think he meant to stay in the loop. The problem seems to be that it doesn't repeat (if you read the original post)
So the problem, I suspect, is that somewhere in there he sets him up to loop through a bunch of false conditions, which means he goes into a hang.

Or it might be something else, but I'm not going looking for it.



yeah you're right i don't see how the while (contd) statement without a break can cause a problem. my issue was why after line 104 it didn't go back to line 012 and follow through.


anyway will try to break it up n see if i can figure it out.

thanks for all the suggestion!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2