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(); } }
Home »
» Generate Alpha Numeric String for the given size
No comments:
Post a Comment