All TalkersCode Topics

Follow TalkersCode On Social Media

Generate Random String Java

Last Updated : Jul 1, 2023

Generate Random String Java

In this article we will show you the solution of generate random string java, the CharSet method creates a byte array of random bytes, converts it to a String with a specified character encoding (for example, "UTF-8") via the Charset.forName() method, and then filters the result to only contain alphanumeric characters (that is, letters and digits).

This technique makes that the final String has a random assortment of letters and digits, making it useful for a variety of tasks like creating passwords, distinctive IDs, and random data for testing.

We'll now discuss the generate random string in Java idea.

Step By Step Guide On Generate Random String Java :-

import java.util.*;
import java.nio.charset.*;
class RandomString {
static String getAlphaNumericString(int n)
{
byte[] array = new byte[256];
new Random().nextBytes(array);
String randomString
= new String(array, Charset.forName("UTF-8"));
StringBuffer r = new StringBuffer();
for (int k = 0; k < randomString.length(); k++) {
char ch = randomString.charAt(k);
if (((ch >= 'a' && ch <= 'z')
 || (ch >= 'A' && ch <= 'Z')
 || (ch >= '0' && ch <= '9'))
 && (n > 0)) {
 r.append(ch);
 n--;
}
}
return r.toString();
}
public static void main(String[] args)
{
int n = 20;
System.out.println(getAlphaNumericString(n));
}
}
  1. Here, you can see how we write the Java code to use the CharSet function to build a random AlphaNumeric String.
  2. The relevant libraries are imported at the beginning of the code: java.util.* for the Random class and java.nio.charset.* for the Charset class.
  3. The RandomString class is then defined using a static method called getAlphaNumericString, which accepts an integer n as input and denotes the required length of the random string.
  4. A byte array with the name array and a size of 256 is made inside the getAlphaNumericString function.
  5. Afterward, the array is filled with a random number using the nextBytes procedure of the Random class, which is called with an argument.
  6. After that, the array is turned into a String by using the String constructor, which requires a Charset object denoting the character encoding to be applied in addition to the byte array. Here, the "UTF-8" encoding is applied.
  7. To save the generated random string, a StringBuffer object called r is constructed.
  8. The for loop is then used to run through each character in the randomString.
  9. It determines whether each character is an alphanumeric character and if the required length n is still more than 0.
  10. The character is added to the r StringBuffer object if it satisfies the alphanumeric requirement and n is larger than 0, and n is decremented by 1 if it does.
  11. After converting the r StringBuffer object to a String using the toString() function, the created random alphanumeric string is returned as the outcome of the getAlphaNumericString method.
  12. The intended length of a random alphanumeric string is represented by the initialization of the integer n in the main method with the value 20.
  13. The produced random string is then sent to the console by calling the getAlphaNumericString function with n as an argument and using the System.out.println command.

Conclusion :-

As a result, we were able to understand how to produce random strings in Java.

We also discovered that the random String created by using a byte array, character encoding, and a filter for alphanumeric characters may be used for a variety of things, including creating passwords, unique identifiers, and random data in Java programmes.

I hope this article on generate random string java helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials