• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sabin mash
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or if you want more flexibility then you can replace your array with an ArrayList
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mwenya: Please UseCodeTags when you post source code.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do I create the book array
 
Campbell Ritchie
Sheriff
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've already told you that. In the constructor.
 
Ryan Matze
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff,

Thanks, that was the problem.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic