English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I)/O)

Java Reader/Writer

Other Java topics

Java program generates alphanumeric random strings

Java Examples Comprehensive

In this example, we will learn how to generate random strings and alphanumeric random strings in Java.

Example1:A Java program for generating random strings

import java.util.Random;
class Main {
  public static void main(String[] args) {
    //Create a string containing all A-String of Z characters
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    //Create a random string generator
    StringBuilder sb = new StringBuilder();
    //Create an object of the Random class
    Random random = new Random();
    //Specify the length of the random string
    int length = 7;
    for(int i = 0; i < length;++) {
      //Generate a random index number
      int index = random.nextInt(alphabet.length());
      //Get the character specified by index index
      //From the string
      char randomChar = alphabet.charAt(index);
      //Attach the character to the string generator
      sb.append(randomChar);
    }
    String randomString = sb.toString();
    System.out.println("The randomly generated string is: " + randomString);
  }
}

Output Result

Random string is: IIYOBRK

In the above example, we first created a string containing all the letters. Next, we used the Random class's nextInt() method to generate a random index number.

Using a random index number, we generated a random character from the string letters. Then, we used the StringBuilder class to concatenate all characters together.

If you want to change the random string to lowercase, you can use the String's toLowerCase() method.

randomString.toLowerCase()

Note:The output will be different each time you run the program.

Example2:A Java program for generating random alphanumeric strings

import java.util.Random;
class Main {
  public static void main(String[] args) {
    // Create a string composed of uppercase and lowercase letters and numbers
    String upperAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String lowerAlphabet = "abcdefghijklmnopqrstuvwxyz";
    String numbers = "0123456789";
    //Merge all strings
    String alphaNumeric = upperAlphabet + lowerAlphabet + numbers;
    //Create a random string generator
    StringBuilder sb = new StringBuilder();
    //Create an object of the Random class
    Random random = new Random();
    //Specify the length of the random string
    int length = 10;
    for(int i = 0; i < length;++) {
      //Generate a random index number
      int index = random.nextInt(alphaNumeric.length());
      // Get the character at index index from the string
      char randomChar = alphaNumeric.charAt(index);
      // Append the character to the string builder
      sb.append(randomChar);
    }
    String randomString = sb.toString();
    System.out.println("The randomly generated string is: " + randomString);
  }
}

Output Result

The randomly generated string is: pxg1Uzz9Ju

Here, we created a string that contains the characters from0 to9thenumbers as well as uppercase and lowercase letters.

From the string, we randomly generated a string of length10alphanumeric string.

Java Examples Comprehensive