

And here is a little program where you can creatre a random String or StringBuffer with just characters.
1 public class helloworld {
2
3 /**
4 * Just runs the method. If a arg is given with that if not with 16 chars
5 */
6 public static void main(String[] args) {
7 System.out.println(getRand((args.length > 0)
4 ? (Integer.parseInt(args[0])) : 16));
9 }
10
11 /**
12 * Return a random string
13 *
14 * @param how long the string should be
15 * @return the string could be a StringBuffer for performace
16 */
17 public static String getRand(int howLong){
18 /* Use string buffer as String+= is very slow */
19 /* Read Effective Java by Bloch, Josh*/
20 StringBuffer returnBuffer = new StringBuffer();
21 for (int i = 0; i < howLong ; i++) {
22 returnBuffer.append ((char) (Math.random() * 26 + 'a'));
23 }
24 return returnBuffer.toString();
25 }
26 }
27
Code coloring and highlighting done with http://alexonasp.net/codehighlighter/Default.aspx
No comments:
Post a Comment