java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to create a repeated sequence of character?

import java.util.Arrays;

public class RepeatCharacter
{
    public static void main(String[] args)
    {
        char c = 'x';
        int length = 10;

        // creates char array with 10 elements
        char[] chars = new char[length];

        // fill each element of chars array with 'x'
        Arrays.fill(chars, c);
        
        // print out the repeated 'x'
        System.out.println(String.valueOf(chars));
    }
}