All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Generate Random String In Java

Last Updated : Mar 11, 2024

Generate Random String In Java

In this article we will show you the solution of generate random string in java, the technique of constructing a string of characters which are randomly chosen from a preset set of characters is known as "generating a random string" in Java.

The string that is created seems random and unpredictable. This is frequently employed in a number of applications, including those that generate passwords, create distinctive IDs, or simulate random data.

Java offers methods like Random and StringBuilder that can be used to accomplish this.

The Random class generates random numbers, & the StringBuilder class creates the random string by inserting characters chosen at random. We'll talk about the Java notion of creating random strings.

Step By Step Guide On Generate Random String In Java :-

import java.util.Random;
class Main {
  public static void main(String[] args) {
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    StringBuilder sb = new StringBuilder();
    Random random = new Random();
    int length = 7;
    for(int i = 0; i < length; i++) {
      int index = random.nextInt(alphabet.length());
      char randomChar = alphabet.charAt(index);
      sb.append(randomChar);
    }
    String randomString = sb.toString();
    System.out.println("Random String is: " + randomString);
  }
}
  1. You can see that we wrote java code to generate random strings here.
  2. We begin our code by using an import line to bring in the java.util library.Java's Random class makes it possible to produce random numbers.
  3. The Java program's entry point is then defined by a class called Main.
  4. The we declare and initialise numerous variables inside the main procedure. All capital alphabets are kept in a string called alphabet.
  5. The random string will be created using the StringBuilder object sb. This will produce random numbers using the random object, which is called random.
  6. The desired random string's desired length, length, is an integer that is set to 7.
  7. The for loop is then used to produce 7 random characters by iterating 7 times (as determined by the length).
  8. The function random.nextInt(alphabet.length()) inside the loop creates a random index between the lengths of the alphabet string.
  9. Then, using the random uppercase alphabet, we use alphabet.charAt(index), which returns the character at the produced index.
  10. The sb StringBuilder object is then appended with this random character using the sb.append(randomChar) method.
  11. When the loop is finished, the desired random string is stored in the sb StringBuilder object.
  12. After that, sb.toString() is used to convert the StringBuilder object into a String object, which is then saved in the randomString variable.
  13. Finally, the created random string and the message showing that it is the "Random String" are printed using System.out.println().

Conclusion :-

Thus, we were able to understand how to produce random strings in Java.

We also discovered that using Java to generate random strings provides a practical method for producing randomised data, such as passwords or distinctive IDs.

To make creating random strings simple and quick, Java offers methods like Random and StringBuilder.

The generated strings can be made to be truly random and unexpected by using these classes, which developers can use.

This method is frequently employed in a number of applications and is still a crucial component of many Java programmes today.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪