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 :)

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That doesn't actually create a string of dots instead it just would print a bunch of them.
@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.
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 "." + "." = ".."

2 Comments

String concatenation in a loop is inefficient.
True. It creates (and then discards) a StringBuilder under the covers.
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.

Comments

1
char[] letters = new char[ 30 ];
Arrays.fill(letters, '.');
String s = new String( letters );

System.out.println( s );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.