• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

How to print % using printf() method?

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Page 489 of SCJP book by Bert and Kathy:

--------------------------------------------------------------
printf("format string", argument(s));

The format string can contain both normal string literal information that isn't asscoaited with any arguments, ...
--------------------------------------------------------------

Now I want to print char % using this printf method. Is this possible?
I have tried System.out.printf("%1$d %", 25); and System.out.printf("%1$d \\%", 25);
It is giving "Exception e: Conversion = '%'"

Is it that we cannot print this special char % using printf/format methods?

Regards
Murali
[ February 07, 2008: Message edited by: Murali Kakarla ]
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I'm afraid you can't use % inside the printf, but that doesn't mean you can use printf to print %. Try this.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can print % following this way-

public class Test{
public static void main(String[]args){

System.out.printf("%1$d%%",123);
}
}

Use %% to print % sign.
 
Murali Kakarla
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Prasun
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic