Java examples for java.lang:String Random
generate Random String by case
/*/*from w w w . j a va2 s.co 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. */ //package com.java2s; import java.util.Random; public class Main { public static void main(String[] argv) { int Length = 42; String caseType = "java2s.com"; System.out.println(generateRandomString(Length, caseType)); } 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; } }