FastFoodCoding

Login


Connect with one of the following accounts





Login

Please wait while we redirect...

Views: 105   



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(System.in);
        int stringLength = in.nextInt();

        String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

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

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

            System.out.print(alphabet.charAt(randomNumber));

        }
    }
}

Output:

Please enter the string length:
10
AwcNqKwhil



On By


Top Tutorials

Top Questions

Top Articles

Top Blogs

Top News