Java program to generate random string

Views: 1   

Following program shows you how to generate random string.
This program gets string length from user and prints random string of the given length

import java.util.Random;
import java.util.Scanner;

public class Basic11 {

    public static void main(String[] args) {

        System.out.println("Please enter the string length:");

        Scanner in = new Scanner(;
        int stringLength = in.nextInt();

        String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

        for (int i = 1; i <= stringLength; i++) {

            Random rnum = new Random();
            int randomNumber = rnum.nextInt(52);




Please enter the string length:
On By