*
The moose likes Beginning Java and the fly likes Array List is Printing Out the Exact Same Thing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array List is Printing Out the Exact Same Thing" Watch "Array List is Printing Out the Exact Same Thing" New topic
Author

Array List is Printing Out the Exact Same Thing

Toshiro Hitsuguya
Greenhorn

Joined: May 15, 2009
Posts: 19




My problem is that whenever I run this everything runs fine. Except when I print the avalible books, it prints the exact same book 4 times. I know that it creates an array, because it prints out four times. However, I can't figure out why it prints out the last book added only.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No, there are four books in your example. Your problem is not that, your problem is that each book should have its own title and its own "borrowed" flag. But your Book class doesn't work like that. I will just give you a hint that the problem is with the declaration of those two fields.
Toshiro Hitsuguya
Greenhorn

Joined: May 15, 2009
Posts: 19
Thanks a lot Phil. I got my code up and running and works fine. I am having a weird error. I put the firstLibrary.printAddress() into System.out.println() and it is printing out null. I was looking at the structure of my Book class for getTitle() and the Book constructor and it is the exact same structure.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

Brian Wimpsett wrote:I am having a weird error. I put the firstLibrary.printAddress() into System.out.println() and it is printing out null. I was looking at the structure of my Book class for getTitle() and the Book constructor and it is the exact same structure.


Well, a good place to look is at the constructor...



Care to explain what you are trying to do?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Toshiro Hitsuguya
Greenhorn

Joined: May 15, 2009
Posts: 19
Sure, I am trying to set the value of address to the argument in the constructor.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Brian Wimpsett wrote:Sure, I am trying to set the value of address to the argument in the constructor.

Are you sure?


I love this place!
Toshiro Hitsuguya
Greenhorn

Joined: May 15, 2009
Posts: 19
Never mind. I had the values reversed. it should be address = street Thanks anyway.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array List is Printing Out the Exact Same Thing
 
Similar Threads
Having Trouble Referencing a Method from Anonther Method in Another Class
Errors Trying To Compare Strings
Getting Incompatible Types java.lang.String
Problem Adding Different "Book" Objects Into A "Library" Array
Getting a lot of "method cannot be applied to (java.lang.string)"