1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

printf problem

Discussion in 'Software Development' started by dametrius, Oct 30, 2007.

Thread Status:
Not open for further replies.
  1. dametrius

    dametrius Thread Starter

    Oct 30, 2007
    I was wondering if there was a way to use the System.out.printf statement to left justify a String and set aside a number of spaces that the user inputs? for example I know that if you want a String left justified and 5 spaces allocated for it it would be printf("-%5s", string) but is there a way to have ("-%'spaces's", string) where spaces is an integer input by the user?

    this is what I have:
    System.out.printf("| -%", width, "s : %1c\n", _line1, "|");
    but it gives a runtime error.
  2. johnhe


    May 11, 2007
    You can do this by dynamically generating the format string. Here is an example of how you could do this in C:

    char string[] = "123";
    char format[50];
    int spaces = 5;
    sprintf( format, "-%%%ds", spaces );
    printf( format, string );

    John Hensley
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Similar Threads - printf problem
  1. n00e
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/645736

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice