开发者

Convert integer to equivalent number of blank spaces

开发者 https://www.devze.com 2022-12-26 14:23 出处:网络
I was wondering what the开发者_JS百科 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

I was wondering what the开发者_JS百科 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.


I think you meant something like:

    int n = 6;
    String s = String.format("%1$"+n+"s", "");

System.out.format("[%13s]%n", "");  // prints "[             ]" (13 spaces)
System.out.format("[%1$3s]%n", ""); // prints "[   ]" (3 spaces)


You can create a char[] of the desired length, Arrays.fill it with spaces, and then create a String out of it (or just append to your own StringBuilder etc).

import java.util.Arrays;

int n = 6;
char[] spaces = new char[n];
Arrays.fill(spaces, ' ');
System.out.println(new String(spaces) + "!");
// prints "      !"

If you're doing this with a lot of possible values of n, instead of creating and filling new char[n] every time, you can create just one long enough string of spaces and take shorter substring as needed.


This is an easy, but rubbish, way:

 int count = 20;
 String spaces = String.format("%"+count+"s", "");

or filled in

String spaces = String.format("%20s", "");


Straight foward solution:

int count = 20;

StringBuilder sb = new StringBuilder(count);
for (int i=0; i < count; i++){
 sb.append(" ");
}
String s = sb.toString();

StringBuilder is efficient in terms of speed.


Why not loop over the integer and add a space on each iteration?

String spaces = "";
for (int i = 0 ; i < position ; i++) spaces += " ";

And you can use StringBuilder instead of String, if position might get very big and performance is an issue.


Here's an example:

public class NestedLoop {
public static void main (String [] args) {
  int userNum  = 0;
  int i = 0;
  int j = 0;


  while (i<=userNum) {
     System.out.println(i);
     ++i;
     if (i<=userNum) {
     for (j=0;j<i;j++) {
        System.out.print(" ");  
     }
    } 
   }
  return;
  }
}

If we make userNum = 3, the output would be:

0
 1
  2
   3

Hope it would help!


If one were going to use an iterative solution anyway, one might want to do it a more "Groovy" way, like:

def spaces=6
print ((1..spaces).collect(" ").join())

Note that this doesn't work for zero spaces. for that you might want something more like:

print (spaces?(1..spaces).collect(" ").join():"")


//This prints the spaces according to the number in the variable n. So in this case it will print 15 spaces.

int n=15;
System.out.format("%1$"+n+"s", "");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号