Java program to generate random string
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