Convert integer to equivalent number of blank spaces.

Posted by mike on Stack Overflow See other posts from Stack Overflow or by mike
Published on 2010-04-14T05:25:23Z Indexed on 2010/04/14 5:33 UTC
Read the original article Hit count: 212

Filed under:
|

I was wondering what the easiest way is to convert an integer to the equivalent number of blank spaces. I need it for the spaces between nodes when printing a binary search tree. I tried this

 `int position = printNode.getPosition(); 
  String formatter = "%1"+position+"s%2$s\n";
  System.out.format(formatter, "", node.element);`

But I am getting almost 3 times as many spaces compared to the int value of position. I'm not really sure if I am formatting the string right either. Any suggestions would be great! If it makes it clearer, say position = 6; I want 6 blank spaces printed before my node element.

© Stack Overflow or respective owner

Related posts about java

Related posts about homework