Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Print Conditionally using System.out.println?  RSS feed

 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I need to print different outputs using println, here is an example.
The general idea is to print either the project list (projectId + projectname) or the manager's projects (managerName + projectName).
Is there a fast and simple way to do this?


Here is the sample code from top down:



And here is the code from the Projects class:
 
Stefan Evans
Bartender
Posts: 1836
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would go with a seperate formatting function:


This abstracts the formatting logic away from your java bean.
Also the String.format method can be more useful than concatenating strings.

If you want to get really fancy, define an interface of ProjectFormatter and implement two different print methods - one for the project list, one for the manager list.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AhFai Chan wrote:Hi.
I need to print different outputs using println, here is an example.
The general idea is to print either the project list (projectId + projectname) or the manager's projects (managerName + projectName).
Is there a fast and simple way to do this?

What is the criteria you want to use in choosing which print format you want?

I suspect you're heading towards having a base class, perhaps 'project', and a few subclasses each with their own toString() method.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:. . . String.format("Project Id: %s \n\t Project Name: %s \n", project.getProjectId(), project.getProjectName());
. . .
Agree about String#format. I might have used printf myself, which takes the same arguments as String#format. But I never use \n. I always use %n which maintains platform independence.
 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was heading in that direction with multiple "sub-classes"... but ended up with many more classes in my package than I would like.
I was hoping for some really clever way of doing it that the gurus here might have thought of that I didn't.
 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:I would go with a seperate formatting function:
If you want to get really fancy, define an interface of ProjectFormatter and implement two different print methods - one for the project list, one for the manager list.

That is a good idea, I'll work on that for a couple of hours and see what comes of it...
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AhFai Chan wrote:. . .  more classes in my package than I would like. . . .
It is quite difficult to have too many classes. It is very easy to have too few.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:What is the criteria you want to use in choosing which print format you want?.

Without this knowledge it's hard to make a recommendation. I could be as easy as setting up an enum field with a switch statement for the formatting.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!