Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Setting field width System.out.printf

  1. #1
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Setting field width System.out.printf

    How do I set the fields with with a variable and not a constant? This is what I want:
    System.out.printf("%xs\tPlayer Points\tComputer Points\t  Winner\n", "Name");
    Here x is an integer variable.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Setting field width System.out.printf

    You could create the format string as, itself, a formatted string!

    int x = 42;
    String fmt = String.format("%%%ds\tPlayer Points\tComputer Points\t  Winner\n", x);
    System.out.printf(fmt, "Name");

    Notice how %% is used to represent a literal % character in the format. Details in the Formatter API docs.

    -----

    The width argument allows an alternative to your use of the \t character whose exact behaviour may or may not be what you want or expect. In particular it may not do the same thing in all circumstances (console, text component, web page etc).

    Also the %n specifier provides an alternative to the \n character and will do the right thing to produce a newline whatever the OS.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    ranjithfs1 (March 18th, 2012)

  4. #3
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Setting field width System.out.printf

    That was helpful! Thanks! :-)

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Setting field width System.out.printf

    You're welcome.

Similar Threads

  1. How to align text? Printf?
    By shifat96 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 20th, 2012, 12:51 PM
  2. Setting constraints on a field
    By cslx99 in forum Object Oriented Programming
    Replies: 11
    Last Post: December 8th, 2011, 03:49 AM
  3. Replies: 3
    Last Post: April 11th, 2011, 09:51 PM
  4. about printf please help having errors
    By Macgrubber in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 11:01 PM
  5. The printf() method explanation needed
    By darek9576 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 14th, 2010, 12:11 AM