
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Create random strings
In this article, we will understand how to create random strings. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”).
Below is a demonstration of the same −
Suppose our input is −
The size of the string is defined as: 10
The desired output would be −
Random string: ink1n1dodv
Algorithm
Step 1 - START Step 2 - Declare an integer namely string_size, a string namely alpha_numeric and an object of StringBuilder namely string_builder. Step 3 - Define the values. Step 4 - Iterate for 10 times usinf a for-loop, generate a random value using the function Math.random() and append the value using append() function. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
public class RandomString { public static void main(String[] args) { int string_size = 10; System.out.println("The size of the string is defined as: " +string_size); String alpha_numeric = "0123456789" + "abcdefghijklmnopqrstuvxyz"; StringBuilder string_builder = new StringBuilder(string_size); for (int i = 0; i < string_size; i++) { int index = (int)(alpha_numeric.length() * Math.random()); string_builder.append(alpha_numeric.charAt(index)); } String result = string_builder.toString(); System.out.println("The random string generated is: " +result); } }
Output
The size of the string is defined as: 10 The random string generated is: ink1n1dodv
Example 2
Here, we encapsulate the operations into functions exhibiting object-oriented programming.
public class RandomString { static String getAlphaNumericString(int string_size) { String alpha_numeric = "0123456789" + "abcdefghijklmnopqrstuvxyz"; StringBuilder string_builder = new StringBuilder(string_size); for (int i = 0; i < string_size; i++) { int index = (int)(alpha_numeric.length() * Math.random()); string_builder.append(alpha_numeric.charAt(index)); } return string_builder.toString(); } public static void main(String[] args) { int string_size = 10; System.out.println("The size of the string is defined as: " +string_size); System.out.println("The random string generated is: "); System.out.println(RandomString.getAlphaNumericString(string_size)); } }
Output
The size of the string is defined as: 10 The random string generated is: ink1n1dodv
- Related Articles
- Golang program to create random strings
- Python Program to Random uppercase in Strings
- Java Program to create random BigInteger within a given range
- C++ Program to Create a Random Graph Using Random Edge Generation
- Java Program to Compare Strings
- Java program to generate random numbers
- Java Program to get random letters
- Java Program to Compare Two Strings
- How to create array of strings in Java?
- Java Program to generate random numbers string
- Java Program to format strings into table
- Java Program to compare strings for equality
- Java Program to get timezone id strings
- Java Program to Add Two Binary Strings
- Program to Compare two strings in Java

Advertisements