Member Avatar for ragnarok511

I am writing a program that simulates a simple library. It compiles with no exceptions. However, it keeps printing when the printAvalibleBooks() method is called. the exact same book even though I have a method that is supposed to poplalate the array here is what I have so far.

public class Library {
	Book[] books = new Book[20];;
	int numBooks = 0;
	String address;
	static String hours;
	
	
	
public Book addBook(String bookTitle) { //This where I am having trouble populating my array
	books[numBooks] = new Book(bookTitle);
	++numBooks;
	return books[numBooks];
	} 


 public boolean borrowBook(String title) { //Marks the book as borrowed  
	if (Book.title == Book.getTitle()){
	Book.borrowed = true;} //refers to a method in another class I created 
	return Book.borrowed;
     }  
   
 public boolean returnBook(String title) { //Marks the book as returned  
		if (Book.title == Book.getTitle()){
	Book.borrowed = false; }//refers to a method in another class I created     
	return Book.borrowed;
     }   

public Library(String street) { //creates new street address
	street = address;
}


public String printAddress() { //returns the street address when referenced
return address;
}

void printAvailableBooks() { //supposed take a book out of the list if borrowed
	for(int i = 0; i < numBooks; i++) {
	if (i < 0) {
	System.out.println("There are no books avalible");
	break;
	}
	else System.out.println(books[i].getTitle());
	} 
}

static String printOpeningHours() {
	hours = "We are open from 9-5.";
	return hours;	
}	


public static void main(String[] args){



Library firstLibrary = new Library("10 Main St."); // Create two libraries
Library secondLibrary = new Library("228 Liberty St.");

firstLibrary.addBook("The Da Vinci Code");// This is where I am getting my exception
firstLibrary.addBook("Le Petit Prince");
firstLibrary.addBook("A Tale of Two Cities");
firstLibrary.addBook("The Lord of the Rings");

System.out.println("Library hours:"); // Print opening hours and the addresses
printOpeningHours();
System.out.println(hours);

System.out.println("Library addresses:");
firstLibrary.printAddress();
secondLibrary.printAddress();

// Try to borrow The Lords of the Rings from both libraries
System.out.println("Borrowing The Lord of the Rings:"); 
firstLibrary.borrowBook("The Lord of the Rings"); 
firstLibrary.borrowBook("The Lord of the Rings"); 
secondLibrary.borrowBook("The Lord of the Rings"); 

// Print the titles of all available books from both libraries
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();


System.out.println("Books available in the second library:");
secondLibrary.printAvailableBooks();
// Return The Lords of the Rings to the first library
System.out.println("Returning The Lord of the Rings:"); 
firstLibrary.returnBook("The Lord of the Rings"); 

// Print the titles of available from the first library
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks(); 
 
	} 

}

Thanks.

Recommended Answers

All 4 Replies

I see a problem but this line doesn't belong!

if (i < 0) {
	System.out.println("There are no books avalible");
	break;
	}

also at the top of your code...
You're returning the garbage pointer after the allocated buffer.

books[numBooks] = new Book(bookTitle);
//	++numBooks;
//	return books[numBooks];   <--- This line is wrong
               return books[ numBooks++ ];
Member Avatar for ragnarok511

Thanks. I tried that and it is still printing out lord of the rings for all titles.

Looks like there are a lot of things wrong. Here is my contribution:
Base: (formated with NetBeans)

void printAvailableBooks() { //supposed take a book out of the list if borrowed
        for (int i = 0; i < numBooks; i++) {
            if (i < 0) {
                System.out.println("There are no books avalible");
                break;
            } else {
                System.out.println(books[i].getTitle());
            }
        }
    }

So first, the if tests if i is less than 0. i is your loop variable, it goes from 0 upwards till numBooks, it will never be less than 0. But if you want to test if there is any book, then you do it with numBooks! Also there is no need to test it every time inside the loop, but you have to test it before the loop if it can run or not. And equality also means there is no book. Also if you want to finish the whole method, use return, it's easier to see. So:

void printAvailableBooks() { //supposed take a book out of the list if borrowed
        if (numBooks <= 0) {
            System.out.println("There are no books avalible");
            return;
        }
        for (int i = 0; i < numBooks-1; i++) {
            System.out.println(books[i].getTitle());
        }
    }

And finally only well designed code can save you from errors! Never write down even the shortest statement twice if it means the same thing, and instead of comments add information directly into the code:

boolean isEmpty() {
        if (this.numBooks <= 0) {
            return true;
        } else {
            return false;
        }
    }

    void printAvailableBooks() { //supposed take a book out of the list if borrowed
        if (this.isEmpty()) {
            System.out.println("There are no books avalible");
            return;
        }
        for (int i = 0; i < this.numBooks-1; i++) {
            System.out.println(this.books[i].getTitle());
        }
    }

(Here the this keywords are not required, and many ppl don't use them, it's just my habit, it makes the code easier to read.)
Maybe you would have noticed it with these easy step.

And please use some code formating programs, like in NetBeans alt+shift+F! This also helps a lot!

ragnarok511,
>There are number of errors:

if (Book.title == Book.getTitle())   <== static or instance?

>Methods - borrowBook, returnBook are not implemented properly.
>Use ArrayList or Vector classes instead of arrays

public class Book
{
   private String title;
   private boolean borrowed=false;
   public Book() {}
   public Book(String v) { title=v; borrowed=false;}
   public void setTitle(String v) { title=v;}
   public String getTitle() { return title;}
   public void setBorrowed(boolean v) {
      borrowed=v;
   }
   public boolean getBorrowed() { return borrowed;}
}
public class Library {
	Book[] books = new Book[20];;
	int numBooks = 0;
	String address;
	static String hours;
	
public Book addBook(String bookTitle) { 
	books[numBooks] = new Book(bookTitle);
	return books[numBooks++];
} 
public boolean borrowBook(String title) {
       int i;
       for(i=0;i<numBooks;i++) {
	if (books[i].getTitle().equals(title))
             break;
         }
       if(i==numBooks) // Book not found
           return false;
       if(books[i].getBorrowed()==false)
          books[i].setBorrowed(true); 
       else
          return false;   
   return books[i].getBorrowed();
 }  
public boolean returnBook(String title) { 
     int i;
     for(i=0;i<numBooks;i++) {
	if (books[i].getTitle().equals(title))
             break;
         }
       if(i==numBooks) // Book not found
           return false;
       if(books[i].getBorrowed()==true)
          books[i].setBorrowed(false); 
       else
          return false;   
  return true;
}   
public Library(String street) { 
  street = address;
}
public String printAddress() {
   return address;
}
void printAvailableBooks() {
	for(int i = 0; i < numBooks; i++)
	  System.out.println(books[i].getTitle());
}
static String printOpeningHours() {
	hours = "We are open from 9-5.";
	return hours;	
}
public static void main(String[] args){
	Library firstLibrary = new Library("10 Main St."); 
	Library secondLibrary = new Library("228 Liberty St.");
	
	firstLibrary.addBook("The Da Vinci Code");
	firstLibrary.addBook("Le Petit Prince");
	firstLibrary.addBook("A Tale of Two Cities");
	firstLibrary.addBook("The Lord of the Rings");

	System.out.println("Library hours:"); 
	printOpeningHours();
	System.out.println(hours);

	System.out.println("Library addresses:");
	firstLibrary.printAddress();
	secondLibrary.printAddress();

	System.out.println("Borrowing The Lord of the Rings:"); 
	firstLibrary.borrowBook("The Lord of the Rings"); 
	firstLibrary.borrowBook("The Lord of the Rings"); 
	secondLibrary.borrowBook("The Lord of the Rings"); 

	System.out.println("Books available in the first library:");
	firstLibrary.printAvailableBooks();
	
	System.out.println("Books available in the second library:");
	secondLibrary.printAvailableBooks();

	System.out.println("Returning The Lord of the Rings:"); 
	firstLibrary.returnBook("The Lord of the Rings"); 

	System.out.println("Books available in the first library:");
	firstLibrary.printAvailableBooks(); 
    } 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.