0

what I am looking to do here is create a string based on the value of an int.

I know I am able to create a vale of an in based on a sting via the length() method but was wondering how to do the opposite.

for example I have an int of 30, with that int I would like to create 30 dots ina line based on that int and place it in a println

 {


     while (wordlegnth1 + wordlegnth2 < LINELENGTH)

     {

         dots++;
         wordlegnth1++;

     }

     System.out.println (word1 + dots + word2);
   }

at the moment, al the println does is prints the 2 words entered, the int value based on the 2 words entered eg stack24overflow

basically what I would like to do is change that 24 from an int into 24 dots (........................)

many thanks :)

Chris
  • 215
  • 1
  • 5
  • 13
  • possible duplicate of [Padding Strings in Java](http://stackoverflow.com/questions/388461/padding-strings-in-java) – Stephen C Nov 10 '10 at 01:35

4 Answers4

2

This is a special case of wanting to repeat a string (or a character, for that matter) some number of times.

However, there's a simple solution of just looping to output a period dots number of times and printing that directly to the screen:

System.out.print(word1);
for(int d = 0; d < dots; d++){
    System.out.print(".");
}
System.out.println(word2);

Alternatively you can form a string (using StringBuilder) first and then output it:

StringBuilder sb = new StringBuilder(dots);
for(int d = 0; d < dots; d++){
    sb.append('.');
}
System.out.println(word1 + sb + word2);

Note that the StringBuilder's capacity is initialized to dots.

Community
  • 1
  • 1
Mark Elliot
  • 68,728
  • 18
  • 135
  • 157
  • That doesn't actually create a string of dots instead it just would print a bunch of them. – austinbv Nov 10 '10 at 00:29
  • @zobgib: the OP didn't ask to create a string of periods, rather, to output the number of periods corresponding to `dots`, that's what this does, and all it claims to do. – Mark Elliot Nov 10 '10 at 00:30
1

There are many ways to do this.

Here's a cheap and simple one, albeit not very general:

    static final String manyDots = "...........................................";
    ...
    dots = manyDots.substring(0, nosDots);  // if nosDots <= manyDots.length()

Here's a more general version:

    char[] dotChars = new char[nosDots];
    Arrays.fill(dotChars, '.');    
    dots = new String(dotChars);

... which is more or less equivalent to:

    StringBuilder sb = new StringBuilder(nosDots);
    for (int i = 0; i < nosDots; i++) {
        sb.append('.');
    }
    dots = sb.toString();

The Apache Commons StringUtils class has various methods for this kind of thing; e.g. leftPad, rightPad, center and repeat. Calling StringUtils.repeat(".", nosDots) would do exactly what you are trying to do.

Finally, the String.format() method and the Formatter classes do this kind of thing, in a higher level way.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
1
int x - 30;
String dots = "";
for (int i = 0; i < x; i++){
     dots += "."; 
}
System.out.println(dots);

What I am doing is taking the int value and looping that many times. The then += operator concatenates the dots variable. So for instance "." + "." = ".."

austinbv
  • 8,683
  • 6
  • 42
  • 78
1
char[] letters = new char[ 30 ];
Arrays.fill(letters, '.');
String s = new String( letters );

System.out.println( s );
Tag
  • 176
  • 3