November 2005
Intermediate to advanced
304 pages
6h 14m
English
When you're testing Java programs, you may need to randomly generate String data to place into a database or use as test parameters for a method. One way to do this is by generating random
int values and using them to index into an array to create each character, as in the following code:
public String getRandomString(int length) { StringBuffer result = new StringBuffer(); Random rand = new Random(); char[] letters = "abcdefghijklmnopqrstuvwxyz".toCharArray(); for (int i=0; i<length; i++) { result.append(letters[rand.nextInt(26)] ...Read now
Unlock full access