Variable widths in Java's String.format method
This works:
int depth = 5;
String str= "Hello"+ String.format("%"+depth+"s"," ") + "world" + "\n";
System.out.println(str);
It prints 5 while spaces in between.
Hello World.
Please check you code and make sure that depth
is assigned with a valid int
value. Most likely that (invalid value in depth
) is the problem.