Generate Alpha Numeric String for the given size


import java.util.Random; import java.util.Scanner; public class GenerateAlphaNumericStringUsingGivenNo { public static void main(String[] args) { Scanner sc=null; int size=0; System.out.println("Enter the size"); sc=new Scanner(System.in); //taking the size from the keyboard size=sc.nextInt(); //calling the method to generate AlphaNumericString System.out.println(generate(size)); } //method for generating AlphaNumericString public static String generate(int size) { Random rand=null; StringBuffer sb=null; String numAndChars=null; //all string which will be choosen to generate numAndChars="abcdefghijklmnopqrstuvwxyz" +"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +"0123456789"; rand=new Random(); sb=new StringBuffer(""); for(int i=0;i<(size);i++) { //logic to generate AlphaNumeric String int index=(int)(numAndChars.length()*Math.random()); //appending the characters one by one sb.append(numAndChars.charAt(index)); } //returning the AlphaNumeric String return sb.toString(); } }
Share:

No comments:

Post a Comment

Quote of the day

Popular Posts

Featured Post

Collection Framework Overview

Collection:- A collection (sometimes called a container) is simply an object that groups multiple elements into single unit. Collect...

Youtube Page

Facebook Page

Recent Posts

About

I am Shivaji Chandra and I'm a Computer Science Engineer. I love to write poems, jokes and quotes. click here →