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

Discussion in 'Java' started by Shawn, Dec 14, 2006.

  1. Shawn

    Shawn Guest

    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.
    Shawn, Dec 14, 2006
    #1
    1. Advertising

  2. Shawn <> wrote:

    > 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().

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 14, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    4
    Views:
    596
    Martin Ambuhl
    Jun 26, 2004
  2. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,212
  3. ern
    Replies:
    6
    Views:
    347
    Clark S. Cox III
    Jan 18, 2006
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    418
  5. guru
    Replies:
    8
    Views:
    259
Loading...

Share This Page