-4

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!

1

2 Answers 2

3
int length = 10;
System.out.printf("%-" + length + "s %s", b, a);
Sign up to request clarification or add additional context in comments.

1 Comment

Ohh thank you so much :)
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);

1 Comment

@KarleMari i hope this is what you mean

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.