We can make use of the String that holds all the characters that can be a part of the random string to be generated and make use of the java.util.Random class to generate the random string based on the length required.
Program:import java.util.Random;
/**
* Code2care Java Examples
*
* Generate Random String
*
*/
public class JavaRandomStringExample {
public static void main(String[] args) {
JavaRandomStringExample jrs = new JavaRandomStringExample();
System.out.println(jrs.generateRamdonString(15));
System.out.println(jrs.generateRamdonString(25));
System.out.println(jrs.generateRamdonString(35));
System.out.println(jrs.generateRamdonString(45));
}
public String generateRamdonString(int size) {
String digits = "ABCDEFGHIJKLMNOPQRSTUVWZYZabcdefghijklmnopqrstuvwxyz0123456789";
int sizeOfRandomString = size;
StringBuilder randomString = new StringBuilder();
for(int i=1;i<=sizeOfRandomString;i++) {
Random random = new Random();
int position = random.nextInt(digits.length());
randomString = randomString.append(digits.charAt(position));
}
return randomString.toString();
}
}
Output:
7pgxqwiR1TEhZJB
kJexTSwpP6pt6kbTY22LhFxFC
m2kySQF7QLx1JGLR9Kz3CCPHkBFircEE9Jt
esMtxgqWd31UbokPwWqAwqu8sak5aJYTpYyoKdPZYH5q6
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- Fix RabbitMQ: AuthenticationFailureException: ACCESS_REFUSED - Java
- Date Calculator: Add/Subtract - Days/Months/Years - Tools
- Rust: Cargo Init vs Cargo New Command - Rust
- Program 7: Find Difference of Two Numbers - 1000+ Python Programs - Python-Programs
- How to make macOS Terminal window Transparent (or Opaque) - MacOS
- List of All 35 Reserved Keywords in Python Programming Language 3.11 - Python
- [Fix] Ubuntu E: Unable to locate package sudo error - Ubuntu
- How to compare Object and Map in JavaScript with Examples - JavaScript