HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMajor

Printing 80 (or more) times the same character

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
samethemorecharacterprintingtimes

Problem

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

Solution

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.

Code Snippets

private static final String DASHES = 
              "----------------------------------------"
            + "----------------------------------------"
            + "----------------------------------------";
System.out.println(DASHES);
public static final String DASHES = new String(new char[80]).replace("\0", "-");
private static final String repeatChar(char c, int length) {
    char[] data = new char[length];
    Arrays.fill(data, c);
    return new String(data);
}
private static final String DASH80 = repeatChar('-', 80);
private static final String SPACE40 = repeatChar(' ', 40);

Context

StackExchange Code Review Q#47986, answer score: 26

Revisions (0)

No revisions yet.