Coffee and Women Kwote Add |
"I really like Irish Coffees... so I like my women like I like my coffee... full of booze."
- some comedian
|
Personal Profile
|
| Name: Fredric Doddridge |
| Playing: BioShock |
| Reading: Devil in the White City |
|
For some strange reason I've been thinking about strings. No not like String theory in physics (although that is extremely fascinating), the textual strings that we use every day. Have you ever played those word games where someone gives you a random glob of letters and you have to rearrange them to figure out what it spells? Well, I thought it would be cool to create a random letter generator so that I too can make a fun little word game for everyone to play. Woot!
Since I'm such a nice guy I'm going to post the Java source code to this cool little utility so you too can impress your friends and be the life of the party :) Have fun!
import java.util.Arrays; import java.util.Random; import java.util.Scanner;
public class RandomizeExample {
/** * @param args */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Hey, type something in below and I'll make it look cool.");
// We start by creating a variable reference to the string we're going to // randomize. String userInput = scanner.next();
// Then we call the super special method 'randomizeString' that we create // below. // String randomized = randomizeString(userInput); String randomized = randomizeStringAnotherWay(userInput);
// Now we print out a cool message... System.out.println("Hey... you typed in " + userInput + ", and I " + "changed it to " + randomized); }
/** * The method randomizeString takes the string you give it, and randomizes its * order... essentially just scrambling all the letters in a chaotic fashion. */ public static String randomizeString(String toRandomize) {
// Ok, we're going to create some buckets to put our random characters into. // We use a char array because its easy to convert a char[] to a String and // back again. int bucketSize = toRandomize.length(); char[] buckets = new char[bucketSize]; // The above means we declare "buckets" to be a variable of type char[], and // assign "buckets" a new char array that is the same size as the string we're // going to randomize.
// Now since we created a char array we should clear it out first. Most // people do this when they create arrays. Arrays.fill(buckets, '\0'); // Fill it with the \0 character (the null char).
// Now we'll loop through each character in the string we're going to // randomize ("toRandomize"), and stuff it into a random bucket... as // long as that bucket space isn't already taken. Random rand = new Random(); for (int i = 0; i < bucketSize; i++) {
// Get the random bucket index that we want to stuff our next letter into. int randomIndex = rand.nextInt(bucketSize);
// If the random bucket is already taken keep looking until we find one // that is vacant. while (buckets[randomIndex] != '\0') { randomIndex = rand.nextInt(bucketSize); }
// Finally we're going to stuff this bucket with the next letter in // "toRandomize". buckets[randomIndex] = toRandomize.charAt(i); }
// Lastly we're going to return our randomized string to whoever called // this method. return new String(buckets); // see... easy to convert a char[] to String. :) }
/** * I created this method in case someone isn't familiar with arrays and * needed something a bit more simple. */ public static String randomizeStringAnotherWay( String toRandomize ) { // Create a new StringBuffer. This is a basic class for organizing // strings. StringBuffer buffer = new StringBuffer(toRandomize.length()); Random rand = new Random(); // Loop through all of the letters in our string, and stick each // one into a random spot in the buffer. for (int letter = 0; letter < toRandomize.length(); letter++) { // Get a random number that is between 0 and the length // of the new string inside the buffer. int bucket = rand.nextInt(letter + 1); buffer.insert(bucket, toRandomize.charAt(letter)); } // Woo hoo. This should work. return buffer.toString(); } }
|