File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to display objects in array list? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to display objects in array list?" Watch "How to display objects in array list?" New topic
Author

How to display objects in array list?

alan ze
Greenhorn

Joined: Jan 29, 2011
Posts: 7
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

Joined: Jun 13, 2009
Posts: 2227
    
    8
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

Joined: May 29, 2005
Posts: 479
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

Joined: Jul 26, 2007
Posts: 10535
    
    9

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()


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Ralph Cook,
Your post was moved to a new topic.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
I think the correct answer to your problem is both to override toString() and to provide getXXX methods.
alan ze
Greenhorn

Joined: Jan 29, 2011
Posts: 7
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

Joined: May 29, 2005
Posts: 479
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Jan 29, 2011
Posts: 7
Thanks for the help
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to display objects in array list?