Java Program to Generate Random String

Hello, friend Java Program to Generate Random String article We are going to know the topic clearly. The reason for that is that our friends have a lot of doubts. We hope the doubts will be resolved through this article. Let’s go inside this article.

java.util.UUID class can be used to get a random string. Its static randomUUID method acts as a random alphanumeric generator and returns a String of 32 characters.

Java Program to Generate Random String

Java Program to Generate Random String

Generate a random normal string

import java.util.Random;

class Main {
  public static void main(String[] args) {

    // create a string of all characters
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // create random string builder
    StringBuilder sb = new StringBuilder();

    // create an object of Random class
    Random random = new Random();

    // specify length of random string
    int length = 9;

    for(int i = 0; i < length; i++) {

      // generate random index number
      int index = random.nextInt(alphabet.length());

      // get character specified by index
      // from the string
      char randomChar = alphabet.charAt(index);

      // append the character to string builder
      sb.append(randomChar);
    }

    String randomString = sb.toString();
    System.out.println("Random String is: " + randomString);

  }
}

Output

Random String is: XBFNHHQMR

Java Program to Generate Random String

Generate a random alphanumeric string

import java.util.Random;

class Main {
  public static void main(String[] args) {

    // create a string of uppercase and lowercase characters and numbers
    String upperAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String lowerAlphabet = "abcdefghijklmnopqrstuvwxyz";
    String numbers = "0123456789";

    // combine all strings
    String alphaNumeric = upperAlphabet + lowerAlphabet + numbers;

    // create random string builder
    StringBuilder sb = new StringBuilder();

    // create an object of Random class
    Random random = new Random();

    // specify length of random string
    int length = 15;

    for(int i = 0; i < length; i++) {

      // generate random index number
      int index = random.nextInt(alphaNumeric.length());

      // get character specified by index
      // from the string
      char randomChar = alphaNumeric.charAt(index);

      // append the character to string builder
      sb.append(randomChar);
    }

    String randomString = sb.toString();
    System.out.println("Random String is: " + randomString);

  }
}

Output

Random String is: hElp8wNWCGuvJYE

Raed Also: JavaScript Program to Implement a Queue

Final Thoughts

Java Program to Generate Random String We hope the article has resolved your doubts. Also, let us know if you have any doubts and we will resolve your doubts.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment