What is RandomStringUtils.randomAlphabetic() in Java?

What is RandomStringUtils.randomAlphabetic() in Java? Answer

randomAlphabetic() is a static method of the RandomStringUtils class which is used to generate random strings consisting of alphabetic characters. Characters will be chosen from the set of Latin alphabetic characters (a-z, A-Z).

There are two variations to this method.

Variation 1

This variation of the method creates a random string whose length is the number of characters specified.

Variation 2

This variation of the method creates a random string whose length is between the inclusive minimum and the exclusive maximum values provided.

Syntax

// for variation 1
public static String randomAlphabetic(int count)

// for variation 2
public static String randomAlphabetic(int minLengthInclusive, int maxLengthExclusive)

Parameters

For variation 1

count: This is the length of the random string to generate.

For variation 2

minLengthInclusive: This is the inclusive minimum length of the string to generate. maxLengthExclusive: This is the exclusive maximum length of the string to generate.

Return value

The method returns a random string.

How to import RandomStringUtils

The definition of RandomStringUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the RandomStringUtils class as follows:


import org.apache.commons.lang3.RandomStringUtils;

Code

import org.apache.commons.lang3.RandomStringUtils;

public class Main{

    public static void main(String[] args){
        int count = 5;
        System.out.println("The output of RandomStringUtils.randomAlphabetic when the length is " + count + " - " + RandomStringUtils.randomAlphabetic(count));

        int minLength = 5;
        int maxLength = 10;
        System.out.println("The output of RandomStringUtils.randomAlphabetic when the (minlength, maxlength) is (" + minLength + ", " + maxLength + ") - " + RandomStringUtils.randomAlphabetic(minLength, maxLength));
    }
}

Output

The output of the code will be as follows:


The output of RandomStringUtils.randomAlphabetic when the length is 5 - ooDIi
The output of RandomStringUtils.randomAlphabetic when the (minlength, maxlength) is (5, 10) - mWDwxkqDa

Note: The output might differ when the above code is run every time.

Explanation

Example 1

  • count = 5

In the first example, we use the first variation of the method where we specify the exact length of the generated string. The method generates the random string ooDIi of length five, consisting of only alphabets.

Example 2

  • minLength = 5
  • maxLength = 10

In the second example, we use the second variation of the method where we specify the minimum and maximum length of the generated string. The method generates the random string mWDwxkqDa of length six, consisting of only alphabets.

What is RandomStringUtils.randomAlphabetic() in Java? Review:

In our experience, we suggest you solve this What is RandomStringUtils.randomAlphabetic() in Java? and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the What is RandomStringUtils.randomAlphabetic() in Java?

Find on Educative

Conclusion:

I hope this What is RandomStringUtils.randomAlphabetic() in Java? would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *