How to use printf(Java 5.0) to print variable in specified range?

S

Shawn

Hi,

I want to print out two variable values in one line in specified location.
Say, num = 3.14, str = "hello"

I want to print out num at the location between 10 and 15(right
justified) and str at the location between 20 and 30(left justified).

I dont' want to hard-code the spaces, because the spaces in between
depends on the value of num and str and their value can be different
each time.

I don't know Java 5.0 printf can do such a job. I have read its
tutorial, but I cannot find out how to set the location without hard-coding.

Thank you very much.
 
C

Christopher Benson-Manica

Shawn said:
I want to print out two variable values in one line in specified location.
Say, num = 3.14, str = "hello"

format( "%15.5f%-10s", num, str );
I don't know Java 5.0 printf can do such a job. I have read its
tutorial, but I cannot find out how to set the location without hard-coding.

Admittedly I'm not 100% sure the above is correct; the documentation
is a good bit more involved than C's printf().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top