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.
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
Joined: Oct 13, 2005
Notice I used String.format in one of my suggestions. Would that help?