Tool: obtain a combination of random letters and numbers (combination of letters and numbers, combination of letters and numbers), tool-type letters

Source: Internet
Author: User

Tool: obtain a combination of random letters and numbers (combination of letters and numbers, combination of letters and numbers), tool-type letters

Package util; import java. util. random;/***** @ author jkfeng * Get a combination of Random letters and numbers (combination of letters and numbers, combination of letters and numbers) **/public class RandomCharOrNumUtil {public static void main (String [] args) {System. out. println (getCharAndNum (6); System. out. println (getChar (6); System. out. println (getNum (6 ));} /*** get the combination of random denominator and Number * @ param length * @ return the combination of random letters and numbers */public static String getCharAndNum (int length) {String val = ""; Random r Andom = new Random (); String charOrNum = ""; for (int I = 0; I <length; I ++) {if (I = 0) {charOrNum = "char";} else if (I = (length-1) {charOrNum = "num";} else {charOrNum = (random. nextInt (2) % 2 = 0 )? "Char": "num";} if ("num ". equalsIgnoreCase (charOrNum) {// how to obtain the number int num = random between 0-9. nextInt (10); if (num = 0) {num = 2;} if (num = 1) {num = 5;} val + = String. valueOf (num);} else {// how to obtain random characters // 97-122 0-25char char1 = (char) (97 + random. nextInt (26); if (char1 = 'O') {char1 = 'M';} val + = String. valueOf (char1) ;}} val = val. toUpperCase (); return val;}/*** get a random letter combination * @ param length * @ return random letter combination */public static String getChar (int length) {String val = ""; Random random = new Random (); for (int I = 0; I <length; I ++) {// how to obtain random characters // 97-122 0-25char char1 = (char) (97 + random. nextInt (26); val + = String. valueOf (char1);} val = val. toUpperCase (); return val;}/*** obtain a random number combination * @ param length * @ return random number combination */public static String getNum (int length) {String val = ""; Random random = new Random (); for (int I = 0; I <length; I ++) {// how to obtain the number int num = random between 0-9. nextInt (10); val + = String. valueOf (num);} val = val. toUpperCase (); return val ;}}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.