I am currently trying to create a line of 60 stars however I have found several ways to do this.
System.out.println(String.format("%60s", "").replace(' ', '*'));
for(int i=0;i<60;i++)
System.out.print("*");
System.out.println("");
int i=0;
while(i<60) {
System.out.print("*");
i++;
}
What's the best fit? (In terms of time and style)
And if there are any other ways to do this?
static String repeat(final String str, final int n) {
final int len = (str == null) ? 0 : str.length();
if (len < 1 || n < 1) {
return "";
}
final StringBuilder sb = new StringBuilder(len * n);
for (int i = 0; i < n; i++) {
sb.append(str);
}
return sb.toString();
}
System.out.println(repeat("*", 60));
AND
System.out.println(new String(new char[60]).replace("\0", "*"));
source
share