Archive

Archive for February, 2013

Generating an overly Complex String :)

February 13, 2013 Leave a comment

This is how i generated an overly “complex” string. There would be surely better implementations but this one works fine for me. 🙂 

package com.intuira.test;

import java.util.Random;

/**
 * @author Hassan Ali
 */
public class OverlyComplexString {
   
    private static String[] alphabets = {"a", "B", "c", "D", "e", "F", "G", "h", "i", "j", "k", "L", "M", "N", "O", "p", "Q", "r", "S", "T", "u", "v", "W", "x", "Y", "z"};
    private static String[] numerals = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
    private static String[] specialChars = {".", ",", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "+", "=", "_", ":", ";", "'", "\"", "/", "<", ">"};
    private static Random random = new Random();

    
/**
 * returns an overly complex randomly generated String 😉
 */
    private static String getOverlyComplexString(int length) {
        
        StringBuilder overlyComplexString = new StringBuilder();
        
        for(int i=0; i<length; i++) {

            
            switch(random.nextInt(3)) { 
            
                case 0:
                    overlyComplexString.append(alphabets[random.nextInt(alphabets.length)]);
                    break;
                
                case 1:
                    overlyComplexString.append(numerals[random.nextInt(numerals.length)]);
                    break;
                    
                case 2:
                    overlyComplexString.append(specialChars[random.nextInt(specialChars.length)]);
                    break;
                
            }
        
        }
        
        return overlyComplexString.toString();
    }
    
    public static void main(String[] args) { 
    
        System.out.println(getOverlyComplexString(20));
        
    }
    
}
Advertisements
Categories: Java