Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

printf wildcards

 
Joe carco
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was wondering if there is an easy way to print an arbitrary amount of arguments using the printf() method using the Printwriter class. I was thinking of doing something like:



but with any amount of arguments. As I dont know how many arguments I want to Format using the printf() method during runtime, I was looking for some kind of '%s' wildcard. Maybe I overlooked something at http://java.sun.com/javase/6/docs/api/java/util/Formatter.html ? Of course I could hardcode something that does this, but it would be so much nicer to use out-of-the-box API calls.

-- carcophan
 
Campbell Ritchie
Sheriff
Pie
Posts: 50240
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.
You can't insert % tags into the format String . . .



But you can create a format String by concatenating Strings, or better with a StringBuilder.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if the number of wildcards depends on the number of arguments, why not just skip the printf and go for StringBuilder altogether?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50240
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would work, Rob, but you might miss formatting in numbers.

How about this?
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works for me.
 
Joe carco
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to JavaRanch.


thanks you!


the reason why I want to use printf is, that an eclipse shortcut I made for logtrace (similar to "systrace" except that it uses log4j) automatically generates a comma separated list of parameters representing the parameters in the method that is meant to be log-traced. Unfortunately, log4j doesn't support a feature similar to printf. So I figured that if I could use printf (which takes any number of comma separated arguments), and pass it on to the log.debug() method I could use this in any method regardless of the number of if parameters.

This is what is currently being generated when i use my logtrace shortcut in a method in Eclipse IDE:



Obviously this doesnt work, becase I dont know how to tell eclipse how to generate "+a+","+b+" ... instead of "+a,b,c,d
Using some sort of wildcard in printf that formats any amount of arguments would help. I didnt want to start writing a method that does this via StringBuilder, because I would have to import it as a library in every project I want to use it in
 
Campbell Ritchie
Sheriff
Pie
Posts: 50240
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice I used String.format in one of my suggestions. Would that help?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic