Print name 10 times
In this article, we will explore different ways to print a name or a particular string 10 times in java with examples.
Though, the examples print the string 10 times, but they can be used to print a string n times or any number of times.
A string can be printed 10 times or n times using a
for
loop as shown below
String s = "codipppa"; for(int i = 0; i < 10; i++ { System.out.println(s); }
This loop will run for values of i
from 0 to 9 or 10 times. In every iteration, it will print the string.
Above example can be written using a while
loop as
String s = "codipppa"; int i = 0; while(i < 10) { System.out.println(s); i++; }
Method 2: Using recursion
This method uses recursion, where a function calls itself. This function shall print the name.
In order to print a name 10 times, we need to call this function 10 times. For this, we pass a count as argument to this method and we check the count.
If the count reaches 10, the function does nothing and returns, otherwise it prints the name and calls itself with increased count. Example,
static void print(String name, int count) { if(count == 10) { return; } System.out.println(name); print(name, ++count); }
For the first time, this function is called as
print("codippa", 0);
So, this function is invoked for value of count from 0 to 9, that is, 10 times.
java.util.Collections
has an nCopies()
method. It accepts two arguments:1. An integer
2. A string
and returns a list with elements which are copies of string.
This list can be converted to a string with String.join()
as shown below
List<String> copies = Collections.nCopies(10, "codippa"); String join = String.join(" ", copies);
String.join()
takes an Iterable as the second argument and combines its element with the separator passed as the first argument.
This can be written as a one-liner below
String join = String.join(" ", Collections.nCopies(10, "codippa"));
Method 4: Using Arrays.fill()
java.util.Arrays
has a fill()
method with an array as the first argument and a value as second argument. It fills the elements of array with this value.
So, if we create a string array of ten elements and use fill with the required string, the array will contain ten elements with same string value.
This array can be converted to a string with String.join()
as shown below.
String arr = new String[10]; Arrays.fill(arr, "codippa"); String join = String.join(" ", arr);
Method 5: Using IntStream
java.util.stream.IntStream
has a range()
function which is used to generate a stream. range()
takes the start and end range as integers and generates a stream.
This stream can be iterated using a forEach() method. forEach()
accepts an object that implements Consumer interface.
Consumer
interface has only one method that takes a single argument and returns no value. Since Consumer
is a Functional interface, we can pass a Lambda expression to forEach()
. Example,
IntStream.range(0, 10). forEach( str -> System.out.println(str) );
repeat()
method is introduced in String class in java 11.It takes a numeric argument and concatenates the string on which it is invoked this number of times. Example,
String s = "codippa"; String repeated = s.repeat(10);
The methods used in this article display a string n times using a loop and without loop. Hope the article was useful.