Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

printf

Thread view: 
column.column@gmail.com - 22 Mar 2008 13:18 GMT
Hello,

How to print formatted text in java? For example I need to print
int=55 in hex format.

Thank You
Lasse Reichstein Nielsen - 22 Mar 2008 13:33 GMT
> How to print formatted text in java? For example I need to print
> int=55 in hex format.

As you said in the subject: printf

System.out.printf("%2x%n", 55);

<URL:http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html#printf(java.lan
g.String
, java.lang.Object...)>

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

column.column@gmail.com - 22 Mar 2008 17:39 GMT
> System.out.printf("%2x%n", 55);

Is not good, must be object type arguments array. I'm wrong?

printf(String format, Object... args)
A convenience method to write a formatted string to this output stream
using the specified format string and arguments.
Lew - 22 Mar 2008 18:46 GMT
Lasse Reichstein Nielsen wrote:
>> System.out.printf("%2x%n", 55);

> Is not good, must be object type arguments array. I'm wrong?
> printf(String format, Object... args)

And yet it works.  Didn't you try it?

Lasse Reichstein Nielsen gave you the right advice.

It's an interesting exercise to figure out why it works.  (Autoboxing)

<http://java.sun.com/javase/6/docs/api/java/io/PrintStream.html#printf(java.lang.
String,%20java.lang.Object
...)>

> An invocation of this method of the form out.printf(format, args)
> behaves in exactly the same way as the invocation
>   out.format(format, args)

format()'s Javadocs in turn reference
<http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax>

wherein they explain that a '%x' conversion will handle
> ... Java integral types: byte, Byte, short, Short, int and Integer, long, Long, and BigInteger

The Javadocs are a handy source of information.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.