JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS


Program: How to create random string with random characters?


Description:

Write a program to generate random string of length 10 charactors. Every time you call the method, the program should generate random string.


Code:
package com.java2novice.random;

import java.util.Random;

public class MyStringRandomGen {

	private static final String CHAR_LIST = 
		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	private static final int RANDOM_STRING_LENGTH = 10;
	
	/**
	 * This method generates random string
	 * @return
	 */
	public String generateRandomString(){
        
		StringBuffer randStr = new StringBuffer();
        for(int i=0; i<RANDOM_STRING_LENGTH; i++){
        	int number = getRandomNumber();
        	char ch = CHAR_LIST.charAt(number);
        	randStr.append(ch);
        }
        return randStr.toString();
    }
	
	/**
     * This method generates random numbers
     * @return int
     */
    private int getRandomNumber() {
        int randomInt = 0;
        Random randomGenerator = new Random();
        randomInt = randomGenerator.nextInt(CHAR_LIST.length());
        if (randomInt - 1 == -1) {
            return randomInt;
        } else {
            return randomInt - 1;
        }
    }
    
    public static void main(String a[]){
    	MyStringRandomGen msr = new MyStringRandomGen();
    	System.out.println(msr.generateRandomString());
    	System.out.println(msr.generateRandomString());
    	System.out.println(msr.generateRandomString());
    	System.out.println(msr.generateRandomString());
    	System.out.println(msr.generateRandomString());
    	System.out.println(msr.generateRandomString());
    	System.out.println(msr.generateRandomString());
    }
}


Output:
UdX5PwalMI
qj62F7zuoR
ndJUNiHSlh
YohYpF5MfA
mwX9UhI7Ci
bCn5bsk2em
pzMXQDYaDx
<< Previous Program 
blog comments powered by Disqus

List of Random class sample programs:

  1. Basic random number generator.
  2. How to generate random numbers in the given range?
  3. How to generate same random sequence everytime?
  4. How to change Random class seed value?
  5. How to create random string with random characters?
Knowledge Centre
Interface and its usage
Interface is similar to a class which may contain method's signature only but not bodies and it is a formal set of method and constant declarations that must be defined by the class that implements it. Interfaces are useful for declaring methods that one or more classes are expected to implement, capturing similarities between unrelated classes without forcing a class relationship and determining an object's programming interface without revealing the actual body of the class.

About Author

My name is Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me at java2novice@gmail.com or you can comment on the page.

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 6 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2014 by Nataraja Gootooru. All Rights Reserved.