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,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- [Fixed] Git Clone git@github.com: Permission denied (publickey) Could not read from remote repository Fatal error - Git
- Syntax error, parameterized types are only available if source level is 1.5 or greater [Java] - Eclipse
- How to open a new tab in Notepad++ - NotepadPlusPlus
- Convert GMT/UTC timezone in Excel to EST, CST, MST, PST, AST and HST - Microsoft
- [Solution] macOS could not be installed on your computer OSInstall.mpkg appears to be missing or damaged - MacOS
- Merge multiple zip files without unzipping (extracting) - HowTos
- Only Chessboard using HTML and CSS Code Example - CSS
- How to switch to Powershell on Mac Terminal - Powershell