This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Error trying to print array to screen through toString()..  RSS feed

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a person object and added to array through this code..

Person person1 = new Person ("Waria", 6, true);
Greggs.addPerson(person1);


Declared the array to store the person in a seperate class through...

Person employee [] = new Person [50];

And now i am trying to print the users to the screen through the following code:

public void viewMembers()
{
System.out.println(employee.toString());
}


But all i get is "[LPerson;@10b30a7" as the output. Can someone please shed some light on what the problem could be?

Sorry i am new to coding, so i probably didnt make any sense, i'd be happy to explain further if someone didnt understand?

Thanks in advance...
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call toString() on an array reference, it gives a String representation of the array object itself -- not the contents of the array.

The implementation of toString() is inherited from java.lang.Object, so you can check the API to see why you're getting things like "[LPerson;@10b30a7."

To get the array's contents, you need to iterate through the array and call toString() on each element in the array.
 
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to use the toString method, you'll also want to write/override the toString() method for your Person class to format the object's contents in some way. Otherwise you'll get something like you got for the employee array.
[ July 28, 2008: Message edited by: Norm Radder ]
 
Sheriff
Posts: 21560
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by marc weber:
To get the array's contents, you need to iterate through the array and call toString() on each element in the array.


Or call java.util.Arrays.toString(employee);
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!