This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problem Adding Different Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem Adding Different "Book" Objects Into A "Library" Array" Watch "Problem Adding Different "Book" Objects Into A "Library" Array" New topic
Author

Problem Adding Different "Book" Objects Into A "Library" Array

sabin mash
Greenhorn

Joined: Aug 31, 2010
Posts: 11
Hi there!

I'm pretty darn green when it comes to Jva and programming as a whole. I am using a Java Course from MIT Open Courseware to learn it on my own.

QUESTION: I am doing the Library assignment and my question concerns placing 4 different Book objects into an array, using a for loop. Right now the program outputs all the same book. I can't figure out the logic to add each Book into one array index each.

I have been thinking about this one problem I have a lot, and looked up things like adding objects into arrays and null checking. Thing I think might be a part of the solution, however I do not know how to apply these things any further to my specific problem. Which if why I came here

Here is the code that is relevant to the issue:



Current output is as i described above, one book title is being printed, which I think means each array index has been given the same value by the end of the loop in the addBook method:

Books available in the first library:
The Lord of the Rings
The Lord of the Rings
The Lord of the Rings
The Lord of the Rings

Books available in the second library:
No books in catalog


Your help would be greatly appreciated. Thank you for your time.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

It's always good to see someone new learning Java!

Your method addBook() takes a single Book, and adds it to each position in your array. Each time you call it, you wipe out what was there before, and replace it with the new Book. That's why your output shows the last book you added four times.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

You can use an instance variable declared in "Library" Class, which keeps track of number of books getting added ...

For instance, "noOfBooks" instance variable,



So, different books can be placed in Library by this way...


SCJP 6 [SCJP - Old is Gold]
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Or if you want more flexibility then you can replace your array with an ArrayList


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Also note that when you post incomplete code you're forcing people to guess what the rest of it is--in this case it's not a huge deal, but it *is* an additional step that adds needless cognitive overhead.

(Personally, I'd also strongly recommend losing all the "end of ..." comments.)
sabin mash
Greenhorn

Joined: Aug 31, 2010
Posts: 11
You can use an instance variable declared in "Library" Class, which keeps track of number of books getting added ...


Ah I see! Wow that was so simple, I cannot believe I didn't do that in the first place.

I see that the program still functions correctly without the null check here:



Is the null check simply good practice?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yes, otherwise you could add a null in the middle of your array. It's always a good idea to set API expectations.

However, if nulls are explicitly not allowed, it might be better to do a null check and throw a NullPointerException, since it would most likely indicate an unrecoverable programming error.
sabin mash
Greenhorn

Joined: Aug 31, 2010
Posts: 11

David Newton:
Also note that when you post incomplete code you're forcing people to guess what the rest of it is--in this case it's not a huge deal, but it *is* an additional step that adds needless cognitive overhead.

(Personally, I'd also strongly recommend losing all the "end of ..." comments.)


I apologize for the incomplete code. I was wondering about posting all of the code, but I made sure to read the forum guidelines for asking questions, and one of the main points reads:
This is especially important with code. When you have a 100 line program that doesn't work as expected, very few people are going to read through all those lines to help you find the problem.

Try to find the minimal number of lines that still show your problem. If you can reduce the code you need to show to 10 lines, many more people will be willing to take a look at it. Doing so demonstrates that you were willing to ShowSomeEffort when presenting your problem.

Perhaps by not posting incomplete code you mean I should make sure I note where the missing code snippets are. This sounds like a better way to do things in the future. I certainly do not want my questions to be a headache for people. Let me know any other correct protocol for posting code and I will follow that from now on.

As for the "//end comments," I thought this was standard practice too, as i took a basic java class a while back, and this was required in all work we did. I will treat that as an isolated request and lose those end comments from now on. Thank you very much!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Some people like //end comments and some people don't like them. It depends where you work and which book you have read.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

<meta>
I'd have to think that most people would be opposed to them in situations like this, though:There is *so* very little gain. It seems more a relic of times when methods were regularly multiple 80x24 screens long. And if we're going to say it's the end of the if, why not provide actual *useful* information and describe *which* if is ending? If the purpose is to avoid scrolling/looking up to determine where in the code you are, might as well make it explicit:Ugh. Some people may like them, but they're a rare breed, I suspect, and would seem to care more about the process of typing than providing informational content.

Not that I have an opinion about it or anything.
</meta>
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

I'm with David here. I used to always use the "end" comments after long blocks, especially if there were nested levels of loops and branches. Now I consider them to be an indicator that cyclomatic complexity is getting too high, cohesion is probably low, and it's time to refactor.

Mwenya Chongo
Greenhorn

Joined: Dec 15, 2010
Posts: 10


I tried to use the above code but I got the "require array , but book found". so where do I declare the array? How do I go about it?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Welcome to JavaRanch , Mwenya Chongo

I can see nowhere in that code where you would suffer that particular compiler error. Where does it occur?
Do you know how to declare arrays at all? Have you seen them in the Java™ Tutorials or similar? I think in that case you would have to declare it as a field of the class, and initialise it in the constructor.
Mwenya Chongo
Greenhorn

Joined: Dec 15, 2010
Posts: 10
Yes I know how to declare an array. I have tried all sorts of ways I can thing of and its like am drawing a blank. I need help.

so far I came with the code below but please ignore the irrelevant part and help me with the adding of the books. I would appreciate if you gave me a full code. I just teach myself java and I need some coaching.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

Mwenya: Please UseCodeTags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
So, you have a String[] which you are not using, and an ArrayList<Book> which you aren't using. You should be able to create a Book[] array similarly to the String[] array.

By the way: the identifiers myArr and myAr are poor style because they don't make it obvious what they mean, and they are easy to confuse with each other.

And, no we don't give out "full code".
Mwenya Chongo
Greenhorn

Joined: Dec 15, 2010
Posts: 10
Where do I create the book array
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
I've already told you that. In the constructor.
Ryan Matze
Greenhorn

Joined: Feb 21, 2012
Posts: 3
Hi all,

I realize this thread has been inactive for a while but I too am trying to learn Java on my own and I'm also having some difficulties with this problem. Here is what I'm doing and the results I'm getting. The book seems to add ok in addBook() method but it's not printing correctly. I'm expecting the book titles but it's printing things like: Book@4e82701e, Book@558ee9d6, Book@199a0c7c, Book@50a9ae05. It looks like it is printing the location. Here is some of my code...hopefully enough to make it clear what I'm doing.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ryan Matze wrote:I'm expecting the book titles


Why are you expecting that? Java doesn't magically know how you want to display a given class. You have to tell it.

but it's printing things like: Book@4e82701e, Book@558ee9d6, Book@199a0c7c, Book@50a9ae05.


You haven't overridden toString() in Book. Google for java toString example if you're not sure how to do that.
Ryan Matze
Greenhorn

Joined: Feb 21, 2012
Posts: 3
Jeff,

Thanks, that was the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem Adding Different "Book" Objects Into A "Library" Array