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,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- How to get UTC (GMT) time using JavaScript - JavaScript
- [IRCTC] Indian railways official eRail API 1.1 for developers to get train info - HowTos
- 10 Beginners Commands for macOS Terminal Usage - MacOS
- How to get weather details in Command Prompt, macOS or Linux Terminal - HowTos
- [Java Threads] Should we extend Thread Class or implement Runnable interface - Java
- [macOS] NetBeans IDE cannot be installed. Java (JRE) found on your computer but JDK XX or newer is required. - MacOS
- List of Programming Languages Supported by Notepad++ - NotepadPlusPlus
- Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end users experience - Java