• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to display objects in array list?

 
alan ze
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made an array list, and then filled the array with constructed objects, but when I try to display it, it doesn't work.



and instead of displaying 1000, it displays BankAccount@19821f
 
Rob Camick
Ranch Hand
Posts: 2599
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are displaying the BankAccount object. If you want to see something meaningfull then you need to override the toString() method of the BackAccount class. Assuming the "1000" represent the value of a field called accountNumber. The toString() method might look something like:

 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also call a method on the bank account to return a string, or more than one method, and call those. The point is that println expects something that it understands, and if you just hand it a random object, it uses toString on it.

So it could be



and toString would not have to be defined on BankAccount in that case.

rc
 
Maneesh Godbole
Saloon Keeper
Posts: 11061
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph Cook wrote:
...and toString would not have to be defined on BankAccount in that case.


I don't think it is a good idea.
From the API docs of toString()
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.


As you can see it makes lots of sense to override the toString()
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph Cook,
Your post was moved to a new topic.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch, Alan Ze

I would suggest you give your BankAccount class some getXXX methods. Then you could call. . . or similar.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the correct answer to your problem is both to override toString() and to provide getXXX methods.
 
alan ze
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I got it working.

I have another question

How come when I'm using the scanner class for input, the program doesn't stop to wait for the user to enter something for the second input?

 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From http://www.java-made-easy.com/java-scanner.html:


Oh, one last thing, don't try to scan text with nextLine(); AFTER using nextInt() with the same scanner! It doesn't work well with Java Scanner, and many Java developers opt to just use another Scanner for integers. You can call these scanners scan1 and scan2 if you want.


I would personally always input strings, then error-check for input of an int before proceeding.

rc
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with Scanner is that next line means "from wherever we are at present until the next end-of-line character". If you enter a number 123 then push enter, the remainder of the line is very short (a 0-character String). You may need a nextLine() call which you ignore. I wrote about that here a couple of years ago. It is something not well covered in the books I have read.

I couldn't get the previous link to open: try this instead if you have the same problem.
 
alan ze
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic