Java – How to print a name 10 times?

This article shows you different ways to print a name ten times.

1. Looping

1.1 For loop

JavaSample1.java

package com.mkyong.samples;

public class JavaSample1 {

    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            System.out.println("Java ");
        }

    }

}

Output


Java
Java
Java
Java
Java
Java
Java
Java
Java
Java

1.2 While loop

JavaSample2.java

package com.mkyong.samples;

public class JavaSample2 {

    public static void main(String[] args) {

        int i = 0;
        while (i < 10) {
            System.out.println("Java");
            i++;
        }

    }

}

2. Read and Print

This example will read input from the console and print it ten times.

JavaSampleReadPrint.java

package com.mkyong.samples;

import java.util.Scanner;

public class JavaSampleReadPrint {

    public static void main(String[] args) {

        String name = "";
        // read an input and print 10 times
        try (Scanner in = new Scanner(System.in)) {
            System.out.print("Your name: ");
            name = in.nextLine();
        }

        for (int i = 0; i < 10; i++) {
            System.out.println(name);
        }
    }

}

Output


Your name: mkyong
mkyong
mkyong
mkyong
mkyong
mkyong
mkyong
mkyong
mkyong
mkyong
mkyong

3. Recursion

This example will use a recursion loop.

JavaSampleReadRecursion.java

package com.mkyong.samples;

public class JavaSampleReadRecursion {

    public static void main(String[] args) {

        print("mkyong", 10);

    }

    static void print(String name, int times) {

        System.out.println(times + ":" + name);

        if (times > 1) {
            print(name, times - 1);
        }
    }

}

Output


10:mkyong
9:mkyong
8:mkyong
7:mkyong
6:mkyong
5:mkyong
4:mkyong
3:mkyong
2:mkyong
1:mkyong

4. No Loop , No Recursion

This example is funny, and it prints a String 1000 times without looping, just simple math.

JavaNoLoop.java

package com.mkyong.samples;

public class JavaNoLoop {

    public static void main(String[] args) {

        String s1 = "Java\n";
        String s3 = s1 + s1 + s1;
        String s10 = s3 + s3 + s3 + s1;
        String s30 = s10 + s10 + s10;
        String s100 = s30 + s30 + s30 + s10;
        String s300 = s100 + s100 + s100;
        String s1000 = s300 + s300 + s300 + s100;
        System.out.print(s1000);

    }

}

5. Char + String and Replace

JavaCharStrReplace.java

package com.mkyong.samples;

public class JavaCharStrReplace {

    public static void main(String[] args) {

        char[] chars = new char[10];
        String str = new String(chars);
        System.out.print(str.replace("\0", "Mkyong\n"));

    }
}

6. Java 8 String Join

6.1 Collections.nCopies and String.join

JavaStringJoin.java

package com.mkyong.samples;

import java.util.Collections;

public class JavaStringJoinNCopies {

    public static void main(String[] args) {

        System.out.print(String.join("\n", Collections.nCopies(10, "Mkyong")));

    }
}

6.2 Arrays.fill and String.join

JavaStringJoinArray.java

package com.mkyong.samples;

import java.util.Arrays;

public class JavaStringJoinArray {

    public static void main(String[] args) {

        String[] str = new String[10];
        Arrays.fill(str, "Mkyong");
        System.out.println(String.join("\n", str));

    }
}

7. Java 8 IntStream.range

JavaIntStream.java

package com.mkyong.samples;

import java.util.stream.IntStream;

public class JavaIntStream {

    public static void main(String[] args) {

        IntStream.range(0,10).forEach(x->System.out.println("Mkyong"));

    }
}

8. Java 11 Repeat

Java11Repeat.java

package com.mkyong.samples;

public class Java11Repeat {

    public static void main(String[] args) {

        String str = "Mkyong\n";
        System.out.println(str.repeat(10));

    }
}

References

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
2 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
mkyongTolikbest61Pierre Recent comment authors
newest oldest most voted
Pierre
Guest
Pierre

also:

IntStream.of(0,10).forEach(I->System.out.println(“Hello”));

mkyong
Guest
mkyong

IntStream.range , thanks for your comment

Tolikbest61
Guest
Tolikbest61

String.repeat() API [Since Java 11] can be used.
String str = “Java”;
System.out.println(str.repeat(10));

mkyong
Guest
mkyong

Thanks for your comment, article is updated.