aspose file tools*
The moose likes Beginning Java and the fly likes toString method and output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "toString method and output" Watch "toString method and output" New topic
Author

toString method and output

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Could someone explain to me what is wrong and what is currently happening with the output of using the toString( ) method in the Storage class and the output of the storeStudent in the Storage class.



The output I am getting is:

C:\java>java StorageTest

This is what is coming into method: Joe Bloggs Java
Student@11a698a Now stored lp = 0
studentDetail after override: [LStorage;@107077e

This is what is coming into method: Fred Smyth Visual Basic
Student@7ced01 Now stored lp = 1
studentDetail after override: [LStorage;@1ac04e8

This is what is coming into method: Sally Collins History
Student@765291 Now stored lp = 2
studentDetail after override: [LStorage;@26e431

This is what is coming into method: JOHN DOE Java
Student@14f8dab Now stored lp = 3
studentDetail after override: [LStorage;@1ddebc3

This is what is coming into method: JOE DOHN Visual Basic
Student@a18aa2 Now stored lp = 4
studentDetail after override: [LStorage;@194ca6c

This is what is coming into method: Joe Dohn Visual C#
Student@17590db Now stored lp = 5
studentDetail after override: [LStorage;@17943a4

When I was expecting the student name and course which is stored in studentDetail [lp] i.e. Joe Dohn Visual C# after override instead of
[LStorage;@17943a4. I thought the declared toString method would over ride this?
Mark Patrick
Ranch Hand

Joined: Feb 22, 2004
Posts: 51
You have not overridden the toString() method inherited from the Object class.


This is what you actually have in your Storag class. You've actually overloaded toString(), not overridden it.



Which version of 'toString' is being called?


Mark Patrick<br />SCJP 1.4
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi Marlene,

I hope you don't mind a litle general advice. I've been watching your posts, although I don't think I've commented on many so far.

It seems to me that you're still struggling with the concept of an object. When it's time to display a list of Students, you're still thinking in terms of one method to loop through a bunch of data and display it. I know that at least in past iterations of this program, the Student names and other info were being kept in big arrays, and many of the methods took a student index as an argument. Hopefully by now each Student object owns a single String holding the name, and another String holding the course name.

In any case, the toString() method you wrote shows that you're still thinking of Students as aggregate data to be operated on, rather than as entities that themselves perform operations. What you need is a toString() method in the Student class which prints a single Student's information.

There's also the detail that the "real" toString() method -- the one that's used automatically by System.out.println(), and the one you're calling explicitly in storeStudent() -- takes no arguments, but you've defined a method that takes arguments. This isn't overriding at all, but overloading -- really defining a different method altogether that just happens to have the same name.

What you want is a toString that looks something like

[code]
public String toString() {
return name + ": " + course;
}
[code]

in the Student class. Then whenever you say something like

Student s = ...
System.out.println(s);

you'll see

Fred Jones: Chemistry 101

To reiterate: let the individual Student objects know just how to display themselves. Other code that holds a collection of Students can then loop over the collection and ask each one to display itself.

Hope this helps.


[Jess in Action][AskingGoodQuestions]
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218

Could you elaborate a bit more please?

Also confused on the following:

I have my toString method declared as:
public String toString (Student studentDetails [ ])

I noticed you typed the method toString ( )
- with nothing coming into it?

Why? How will this method know the studentDetails then? i.e.
//Method to override toString to print meaninful information
public String toString (Student studentDetails [ ])
{
String result = "";
for (int i=0; i< numberOfStudents; ++i)
{
result += studentDetails[ i];
}
return result;
}//end method toString

(Thanking you in advance)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Your question -- how does the Student know the information? -- is exactly the point that I want to get across to you, so good, we're communicating!
It's also really the crux of object-oriented programming.

I don't know exactly what your code looks like right now, but here's what you ought to be triving for: A Student object should know all the information that's relevant to one Student, and that's all. For the sake of argument, let's say that that's a name and a course. The basic Student class would then look like this:



Then you'd have other classes. The one that maintained a collection of these Student objects might be called "School".



Note that to print all the students, the School just prints each object (each Student) in the _students collection. It's up to the individual Student to render itself as a String. The Student knows how to do this because each Student stores its own name and course in its own individual member variables.

Lots of other things become very easy when you delagate responsibility in this way. For example, let's say you needed a method to remove a Student. You have to give the Student class an equals() method which will allow it to compare itself to another Student a report whether they're the same:



Then you can write the School.removeStudent() method using the remove() method of ArrayList:



All the work of deciding whether the Student to be removed is the same as one in the collection is done by the individual Students themselves, in collaboration with the ArrayList.

Finally, of course, you need an application to work with these classes. Generally you want that to be in yet another class. All this last class has to do is create a School, and call the methods of School, delegating most of the work to School:



I know the details of your code will differ, but what I'm trying to show you here is how to divide up the responsibility for various operations between several classes. If a Student knows about being an individual Student, and a School knows about manipulating a collection of Students, then a main application routine that works with a School can be simple and easy to understand.

I've left out your Storage object, but similarly, it should know only about storing data, accepting Student objects, asking each Student for its data using those getName()/getCourse() methods, and storing the data in some way. It should retrieve data and reconstruct Student objects from it -- it shouldn't pass the raw data back to the school. The school deals in Students, not in lists of Strings.

Hope this all makes sense. You seem like a very hard working person and I'd like to see you getting some payback from that energy rather than being bogged down in details -- which is what happens when you try to do everything at once, rather than delegating responsibility as I've shown here.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Ernest Fridman-Hill,

Firstly, giving me general advice is I will take ALL the advice I can get! Especially when you hit the nail on the head!! Thank you!

As you can guess it took me all of a second after your posting.

The output is NOW as expected:

C:\java>java StorageTest

This is what is coming into method: Joe Bloggs Java
Joe Bloggs : Java Now stored lp = 0

This is what is coming into method: Fred Smyth Visual Basic
Fred Smyth : Visual Basic Now stored lp = 1

This is what is coming into method: Sally Collins History
Sally Collins : History Now stored lp = 2

This is what is coming into method: JOHN DOE Java
JOHN DOE : Java Now stored lp = 3

This is what is coming into method: JOE DOHN Visual Basic
JOE DOHN : Visual Basic Now stored lp = 4

This is what is coming into method: Joe Dohn Visual C#
Joe Dohn : Visual C# Now stored lp = 5



Also understood the overriding - I think we both put a posting at the same time.

Well, I guess I will get on with my assignment (I didn't need this for the assignment as I had to calculate the hash anyway BUT I got caught up in the toString method )

Thanks again (and also thanks to Mike Gershman who has been brilliant with explaining things in detail.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Think we posted this at the same time too!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString method and output
 
Similar Threads
Method to display not working
Loops
Arrays: Calling input from other methods and classes
Arrays and methods - not getting the results I expected
Passing Objects into Methods