# Printing 80 (or more) times the same character

I want to print a lot of the same characters and I don't want to use a for loop and I'm also looking for a way to do it as less lines as possible. Even better I would like to be able to store that string in a static variable to use again and again.

This is how I currently do it (ugly but at least no for loop):

System.out.println("----------------------------------------"
+ "----------------------------------------"
+ "----------------------------------------");


What's a better way (aesthetically wise)?

-
SO dupe : stackoverflow.com/a/1235184/7602 – konijn Apr 23 '14 at 16:05
@konijn didn't notice when I searched, sorry. Still though I think this is a more fitting question for this site now and it wouldn't be bad to have it. – Aki K Apr 23 '14 at 16:08
Rolfl made the question viable, I learned something ;) – konijn Apr 23 '14 at 16:14
In Python this is easily done with '-'*80, haha – justhalf Apr 24 '14 at 1:34

String constants in Java are very efficient. In practice, your code is very high-performing. The issue here is not about how fast the code runs, because, essentially, it cannot be run any faster than this.

If you want to have more efficient code in terms of space, then the practical thing to do is to declare the value as a string constant, something like:

private static final String DASHES =
"----------------------------------------"
+ "----------------------------------------"
+ "----------------------------------------";


And then, in your code, you just:

System.out.println(DASHES);


This will be just as fast, but the code is easier to read.

If the one place where you declare the dashes is still unsightly, then you can compute the value using the other mechanisms shown in other answers... e.g. :

public static final String DASHES = new String(new char[80]).replace("\0", "-");


but that is overkill, and unnecessary.

Edit:

In your comments you raised the issue of having multiple different-length values. I would recommend that you create a constant for the longest one (whether you use the String-constant or some other generator method), and then do a DASHES.substring(0, length) on that to get the shorter constants...

alternatively, I would consider a helper function as useful for this problem....

private static final String repeatChar(char c, int length) {
char[] data = new char[length];
Arrays.fill(data, c);
return new String(data);
}


and then your constants can be initialized with:

private static final String DASH80 = repeatChar('-', 80);
private static final String SPACE40 = repeatChar(' ', 40);


etc.

-
I would argue though that since performance is of no concern (initializing it just once is really no performance hit at all) the second way is better since it is literally one line (makes your code way more concise comparing to 4 lines) and also it is way easier to fine tune the number of characters one could want. – Aki K Apr 23 '14 at 16:31
new String(new char[80]).replace("\0", "-"); now if just something like that would exist in Brainf*ck... – Simon Forsberg Apr 23 '14 at 16:40
@SilliconTouch I would agree with the performance statement (the static initializer is not a performance problem). The real question is whether a string constant is easier to read than the alternatives. Importing a third-party library for a static initializer seems overkill, and the new String(...).replace(...) in itself is complicated too. I am on the fence. But, you should choose whichever is more readable to you. Given that you started with the constant string, it seems to me that is more maintainable for you. – rolfl Apr 23 '14 at 16:40
...and wish you were using C++, so the "concise" version could actually be somewhat concise, like: std::string DASHES(80, '-'); :-) – Jerry Coffin Apr 23 '14 at 16:43
@rolfl It is true that it would not be worth to import a third-party library just for this reason but in my situation I am already using the library in that class so it is not an issue. I think I will prefer the second way because in my situation I am still fine tuning how many dashes I would need so it would be easier to use and for some reason it looks kinda easier to my eyes. Anyway though your answer was really helpful and made me realize things I did not see. – Aki K Apr 23 '14 at 16:46

I wouldn't call this ugly:

System.out.println("----------------------------------------"
+ "----------------------------------------"
+ "----------------------------------------");


There's nothing wrong with it. It's perfectly clear what it does and it's efficient.

If you print many lines like this then put the string in a variable so you can easily print as many times as you want:

private static final String DASHES = "----------------------------------------"
+ "----------------------------------------"
+ "----------------------------------------";


Maybe it's a hassle to create the string (for example, type "-" 10 times and copy and paste that 8 times) but you only have to do it once.

If you really want to do it programmatically, then this seems a relatively nice way (without external libraries):

public static final String DASHES = new String(new char[80]).replace("\0", "-");


Or if you don't mind using StringUtils (from commons-lang) then you might fancy this:

public static final String DASHES = StringUtils.repeat("-", 80);


But like @rolfl, I don't think it's worth the effort to memorize such trickery. You can always just type the thing, there's nothing wrong with it. Also keep in mind Occam's razor: the simplest solution is often the best.

http://stackoverflow.com/questions/1235179/simple-way-to-repeat-a-string-in-java

-
I much prefer seeing StringUtils.repeat("-", 80), which more clearly and verifiably says: 80 hyphens/dashes. I've maintained projects with constants of 80 spaces that turned out not to be 80 spaces. Ne'er again. – JvR Apr 24 '14 at 11:12

Upvoted answers from What is the easiest way to generate a String of n repeated characters? are ...

int n = 80;
char[] chars = new char[n];
Arrays.fill(chars, '-');
String result = new String(chars);


... and ...

If you can, use StringUtils from Apache Commons Lang:

StringUtils.repeat("ab", 3);  //"ababab"

-