This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Calling methods which contain system.out.println code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling methods which contain system.out.println code" Watch "Calling methods which contain system.out.println code" New topic

Calling methods which contain system.out.println code

David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66

I have a method which prints the Type and Value fields from selection of Car objects.

Then I need to total the value of multiple forcourts by calling getForecourtYValue(), getForecourtZValue().
(Don't worry my code is much cleaner than this im trying to provide an example which is unrealted to my code).

And then I will print the combined value of these forecourts. In my summing method I just call each function which adds up each individual forecourt and thus minimises code duplication, but it does print each forecourts listings twice (i'm using Test Driven Design).

How can I avoid a function which is called printing its output?

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11858

I'd change the code. if the method is called getForecourtXValue(), it should only give you the value, not get AND print the value.

Can you create a new method called printForecourtXValue, and then use the two methods when/where you need to?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Have you checked out Aspose?
subject: Calling methods which contain system.out.println code
It's not a secret anymore!