Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array List is Printing Out the Exact Same Thing

 
Toshiro Hitsuguya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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
Sheriff
Posts: 21133
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21197
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Toshiro Hitsuguya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, I am trying to set the value of address to the argument in the constructor.
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Wimpsett wrote:Sure, I am trying to set the value of address to the argument in the constructor.

Are you sure?
 
Toshiro Hitsuguya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind. I had the values reversed. it should be address = street Thanks anyway.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic