Java Random String generateRandomString(int Length, String caseType)

Here you can find the source of generateRandomString(int Length, String caseType)

Description

generate Random String

License

Apache License

Declaration

public static String generateRandomString(int Length, String caseType) 

Method Source Code

//package com.java2s;
/*//from   w  w  w.j  a  v a 2s. c  o m
 * Copyright 2013, The Sporting Exchange Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Random;

public class Main {
    public static String generateRandomString(int Length, String caseType) {

        String[] caseList = new String[Length];
        if (caseType.toUpperCase().matches("UPPER")) {
            for (int i = 0; i < caseList.length; i++) {
                caseList[i] = "UPPER";
            }
        } else if (caseType.toUpperCase().matches("LOWER")) {
            for (int i = 0; i < caseList.length; i++) {
                caseList[i] = "LOWER";
            }
        } else if (caseType.toUpperCase().matches("FIRSTUPPER")) {
            for (int i = 0; i < caseList.length; i++) {
                if (i == 0) {
                    caseList[i] = "UPPER";
                } else {
                    caseList[i] = "LOWER";
                }
            }
        } else if (caseType.toUpperCase().matches("MIXED")) {
            for (int i = 0; i < caseList.length; i++) {
                Random RND = new Random();
                boolean yBool = RND.nextBoolean();
                if (yBool) {
                    caseList[i] = "UPPER";
                } else {
                    caseList[i] = "LOWER";
                }
            }
        } else {
            for (int i = 0; i < caseList.length; i++) {
                Random RND = new Random();
                boolean yBool = RND.nextBoolean();
                if (yBool) {
                    caseList[i] = "UPPER";
                } else {
                    caseList[i] = "LOWER";
                }
            }
        }

        String returnString = "";
        int tempInt;
        char randomChar;
        for (int i = 0; i < caseList.length; i++) {
            if (caseList[i].matches("UPPER")) {
                Random RNG1 = new Random();
                tempInt = RNG1.nextInt(90 - 65 + 1) + 65;
                randomChar = (char) tempInt;
            } else {
                Random RNG2 = new Random();
                tempInt = (char) RNG2.nextInt(122 - 97 + 1) + 97;
                randomChar = (char) tempInt;
            }
            returnString = returnString + String.valueOf(randomChar);
        }

        return returnString;

    }
}

Related

  1. generateRandomString(int length)
  2. generateRandomString(int length)
  3. generateRandomString(int length)
  4. generateRandomString(int length)
  5. generateRandomString(int length)
  6. generateRandomString(int length, String charset)
  7. generateRandomString(int minLength, int maxLength, int minLCaseCount, int minUCaseCount, int minNumCount, int minSpecialCount)
  8. generateRandomString(int n)
  9. generateRandomString(int n)