• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Array List is Printing Out the Exact Same Thing

 
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.
 
Marshal
Posts: 25814
69
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.
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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.
 
Rancher
Posts: 377
Android Spring Java
  • 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.
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic