printf problem

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

dametrius

Thread Starter
Joined
Oct 30, 2007
Messages
1
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.
 
Joined
May 11, 2007
Messages
56
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
www.resqware.com
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Top