Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes printf wildcards Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "printf wildcards" Watch "printf wildcards" New topic
Author

printf wildcards

Joe carco
Ranch Hand

Joined: Apr 14, 2009
Posts: 82
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

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

But if the number of wildcards depends on the number of arguments, why not just skip the printf and go for StringBuilder altogether?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
That would work, Rob, but you might miss formatting in numbers.

How about this?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Works for me.
Joe carco
Ranch Hand

Joined: Apr 14, 2009
Posts: 82
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

Joined: Oct 13, 2005
Posts: 39084
    
  23
Notice I used String.format in one of my suggestions. Would that help?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: printf wildcards