wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes toString Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "toString" Watch "toString" New topic
Author

toString

Zachary T. Anderson
Greenhorn

Joined: Mar 12, 2013
Posts: 16
I have coded a tester file. In the file I need to test my two classes. I am needing my output to look like this:

Zac Anderson ID: 09 Home: Detroit

Score Date Test SAT ACT
75 6/03/12 Lincoln 69.5 123
77 7/23/12 McKinnley 70.4 128

But what I am getting is:
Zac Anderson ID: 09 Home: Detroit

Score Date Test SAT ACT
75 6/03/12 Lincoln 69.5 123
Score Date Test SAT ACT
77 7/23/12 McKinnley 70.4 128

I can't seem to figure out how to get the second string and so forth without the Score, date, test, SAT, and ACT repeating. I know I made Score card and Score card1 but that didn't change anything. So how could I get the first line to print and then it just take in the data. Is this something I need to do in the string of my Score class?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11322
    
  16

what does the toString() method of your Score class look like? I would guess it is returning somthing like this:

Score Date Test SAT ACT
77 7/23/12 McKinnley 70.4 128

so naturally, each time you call it, you get the header.

I would suggest you change it so that it only returns the score, date, test, etc. Perhaps you could add a "getHeader" method to it. Then you call the getHeader once, and then your toString as many times as you need.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Zachary T. Anderson
Greenhorn

Joined: Mar 12, 2013
Posts: 16
That worked to pull the getHeader. Now my output is messing up. I am going to work on it and see what I have done. Thanks.
Zachary T. Anderson
Greenhorn

Joined: Mar 12, 2013
Posts: 16
Here is what I did:



And this is my output:
Malloy ID number: 1001 Home Course: Warren

141Lincoln 08/10/12 67.0 112

116McKinley 09/09/09 78.0 109

It didn't give me a header. I know I'm not doing it right.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11322
    
  16

your getHeader method should probably RETURN the string it creates...and then you should probably use it...
Zachary T. Anderson
Greenhorn

Joined: Mar 12, 2013
Posts: 16
The header works but now the way it prints to screen is wierd.



The output is giving me:
Score Date Testing Location SAT ACT

Malloy IDnum: 1001 Home School: Lincoln

123 08/10/12 Lincoln 67 112
98 09/09/09 McKinley 78 109


I am trying to figure out why the Score Date etc. line is not going into the location I would like it to.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Only us \n in literals if you have been told a CR character is required. For line breaks, use printf and the %n tag.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7825
    
  21

Zachary T. Anderson wrote:The header works but now the way it prints to screen is wierd.

Just a small point: toString() is designed to be used anywhere that you need a string representation of an object, and so should be written very generically.

Your requirement, on the other hand is very specific - You are writing a report, and you want your data formatted specifically for that report.

My advice: Consider creating a CollegeReport class, with toString() (or toLine()) methods for each type of object that the report might need, eg:that way, all your "reporting" logic (including headers, if needed) is in one place, and separate from the objects that make it up.
The advantage of that is that if you ever need to change the report, you don't need to change any of the objects involved.

For more information about this, you might want to read up about "loose coupling".

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString