Printf in Java with variable spaces [duplicate]










-4















This question already has an answer here:



  • print spaces with String.format()

    2 answers



I have a problem with printf in java. I would like to define the spaces with a variable.



 String a = "bar";
String b ="foo";

System.out.printf("%-10s %s", b, a);


//Output:foo*7spaces*bar



The "10" should be changeable. I hope you know what I mean. Thank you guys!










share|improve this question















marked as duplicate by Pradeep Simha, Johan, Tunaki java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jun 14 '16 at 12:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Have a look at this answer: print spaces with String.format().
    – aix
    Jun 14 '16 at 11:30















-4















This question already has an answer here:



  • print spaces with String.format()

    2 answers



I have a problem with printf in java. I would like to define the spaces with a variable.



 String a = "bar";
String b ="foo";

System.out.printf("%-10s %s", b, a);


//Output:foo*7spaces*bar



The "10" should be changeable. I hope you know what I mean. Thank you guys!










share|improve this question















marked as duplicate by Pradeep Simha, Johan, Tunaki java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jun 14 '16 at 12:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Have a look at this answer: print spaces with String.format().
    – aix
    Jun 14 '16 at 11:30













-4












-4








-4


1






This question already has an answer here:



  • print spaces with String.format()

    2 answers



I have a problem with printf in java. I would like to define the spaces with a variable.



 String a = "bar";
String b ="foo";

System.out.printf("%-10s %s", b, a);


//Output:foo*7spaces*bar



The "10" should be changeable. I hope you know what I mean. Thank you guys!










share|improve this question
















This question already has an answer here:



  • print spaces with String.format()

    2 answers



I have a problem with printf in java. I would like to define the spaces with a variable.



 String a = "bar";
String b ="foo";

System.out.printf("%-10s %s", b, a);


//Output:foo*7spaces*bar



The "10" should be changeable. I hope you know what I mean. Thank you guys!





This question already has an answer here:



  • print spaces with String.format()

    2 answers







java eclipse printing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 14 '16 at 11:28

























asked Jun 14 '16 at 11:23









Karle Mari

33




33




marked as duplicate by Pradeep Simha, Johan, Tunaki java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jun 14 '16 at 12:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Pradeep Simha, Johan, Tunaki java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jun 14 '16 at 12:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • Have a look at this answer: print spaces with String.format().
    – aix
    Jun 14 '16 at 11:30
















  • Have a look at this answer: print spaces with String.format().
    – aix
    Jun 14 '16 at 11:30















Have a look at this answer: print spaces with String.format().
– aix
Jun 14 '16 at 11:30




Have a look at this answer: print spaces with String.format().
– aix
Jun 14 '16 at 11:30












2 Answers
2






active

oldest

votes


















0














int length = 10;
System.out.printf("%-" + length + "s %s", b, a);





share|improve this answer




















  • Ohh thank you so much :)
    – Karle Mari
    Jun 14 '16 at 11:35


















0














String a = "bar";
String b ="foo";
int numspace = 10;
String space = String.format("%"+ numspace +"s", " ");

System.out.printf("%s %s %s", b,space, a);





share|improve this answer




















  • @KarleMari i hope this is what you mean
    – Seek Addo
    Jun 14 '16 at 11:35

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














int length = 10;
System.out.printf("%-" + length + "s %s", b, a);





share|improve this answer




















  • Ohh thank you so much :)
    – Karle Mari
    Jun 14 '16 at 11:35















0














int length = 10;
System.out.printf("%-" + length + "s %s", b, a);





share|improve this answer




















  • Ohh thank you so much :)
    – Karle Mari
    Jun 14 '16 at 11:35













0












0








0






int length = 10;
System.out.printf("%-" + length + "s %s", b, a);





share|improve this answer












int length = 10;
System.out.printf("%-" + length + "s %s", b, a);






share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 14 '16 at 11:32









Per Huss

2,560519




2,560519











  • Ohh thank you so much :)
    – Karle Mari
    Jun 14 '16 at 11:35
















  • Ohh thank you so much :)
    – Karle Mari
    Jun 14 '16 at 11:35















Ohh thank you so much :)
– Karle Mari
Jun 14 '16 at 11:35




Ohh thank you so much :)
– Karle Mari
Jun 14 '16 at 11:35













0














String a = "bar";
String b ="foo";
int numspace = 10;
String space = String.format("%"+ numspace +"s", " ");

System.out.printf("%s %s %s", b,space, a);





share|improve this answer




















  • @KarleMari i hope this is what you mean
    – Seek Addo
    Jun 14 '16 at 11:35















0














String a = "bar";
String b ="foo";
int numspace = 10;
String space = String.format("%"+ numspace +"s", " ");

System.out.printf("%s %s %s", b,space, a);





share|improve this answer




















  • @KarleMari i hope this is what you mean
    – Seek Addo
    Jun 14 '16 at 11:35













0












0








0






String a = "bar";
String b ="foo";
int numspace = 10;
String space = String.format("%"+ numspace +"s", " ");

System.out.printf("%s %s %s", b,space, a);





share|improve this answer












String a = "bar";
String b ="foo";
int numspace = 10;
String space = String.format("%"+ numspace +"s", " ");

System.out.printf("%s %s %s", b,space, a);






share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 14 '16 at 11:34









Seek Addo

1,45621227




1,45621227











  • @KarleMari i hope this is what you mean
    – Seek Addo
    Jun 14 '16 at 11:35
















  • @KarleMari i hope this is what you mean
    – Seek Addo
    Jun 14 '16 at 11:35















@KarleMari i hope this is what you mean
– Seek Addo
Jun 14 '16 at 11:35




@KarleMari i hope this is what you mean
– Seek Addo
Jun 14 '16 at 11:35



這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto