Java String Multiplication
How about this??
System.out.println(String.format("%10s", "").replace(' ', '*'));
This gives me output as **********
.
I believe this is what you want...
Update 1
int yournumber = 10;
System.out.println(String.format("%" + yournumber + "s","*").replace(' ', '*'));
Good Luck!!!
Simple way to repeat a string
String::repeat
". ".repeat(7) // Seven period-with-space pairs: . . . . . . .
New in Java 11 is the method String::repeat
that does exactly what you asked for:
String str = "abc";
String repeated = str.repeat(3);
repeated.equals("abcabcabc");
Its Javadoc says:
/**
* Returns a string whose value is the concatenation of this
* string repeated {@code count} times.
* <p>
* If this string is empty or count is zero then the empty
* string is returned.
*
* @param count number of times to repeat
*
* @return A string composed of this string repeated
* {@code count} times or the empty string if this
* string is empty or count is zero
*
* @throws IllegalArgumentException if the {@code count} is
* negative.
*
* @since 11
*/
Simple way to repeat a string
String::repeat
". ".repeat(7) // Seven period-with-space pairs: . . . . . . .
New in Java 11 is the method String::repeat
that does exactly what you asked for:
String str = "abc";
String repeated = str.repeat(3);
repeated.equals("abcabcabc");
Its Javadoc says:
/**
* Returns a string whose value is the concatenation of this
* string repeated {@code count} times.
* <p>
* If this string is empty or count is zero then the empty
* string is returned.
*
* @param count number of times to repeat
*
* @return A string composed of this string repeated
* {@code count} times or the empty string if this
* string is empty or count is zero
*
* @throws IllegalArgumentException if the {@code count} is
* negative.
*
* @since 11
*/
Print a String 'X' Times (No Loop)
You can use recursion like this
private void printStar(int n){
if(n > 0){
System.out.print("*");
printStar(n-1);
}
}
And call the method like this initially - printStar(4);
Related Topics
What Is the Cross-Platform Way of Obtaining the Path to the Local Application Data Directory
Hiding Instance Variables of a Class
How to Update Path Variable Permanently from Windows Command Line
How to Get Data Between Quotes in Java
Running Rmi Server, Classnotfound
How to Split String with Some Separator But Without Removing That Separator in Java
Why Does Concurrenthashmap Prevent Null Keys and Values
No-Throw Virtualmachineerror Guarantees
Missing Return Statement for If/Else Statement
Why Business Logic Should Be Moved Out of Jsp
Calendar Returns Date in Wrong Time Zone
Capturing Image from Webcam in Java
How Do Java Interfaces Simulate Multiple Inheritance
Hadoop No Filesystem for Scheme: File
How to Add an Image to a Jbutton
Difference in System. Exit(0) , System.Exit(-1), System.Exit(1 ) in Java