It’s usually necessary to have a way to produce random strings programmatically:

Today, I will show you a very quick way to create those strings for studying purpose or as a simple solution.

However, I do really recommend you to use RandomStringUtils API from Apache library commons-lang3 to use in your projects for production. This library covers many possibilities and you will avoid adding any unnecessary class into your project. The library can be added into your pom.xml with the following dependency.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
    <type>jar</type>
</dependency>

Back to this simple project, it was compiled using Java 8, but you can compile also in previous versions of Java (maybe some adjustments will be necessary).

This code is also available in my GitHub page. Feel free to comment if you have any doubt or suggestion. 😄

Licensing

This software is licensed under the MIT license, what basically means that you can use it for any purpose without any warranty. ⚠️

Architecture

This is a single class application, which has a method that returns the new String, or throws an exception IllegalArgumentException in case the length requested is not greater than 0.

The application will iterate over an array of letters and numbers and will return the amount of requested characters.

Code

I’m not attaching here the Main.class, which is only an class to exemplify with the execution of the application. If necessary, visit my GitHub page.

StringGenerator.class

Tags: java 
comments powered by Disqus