-
Word Jumbler
Hi, i am having trouble figuring out how to create a word jumbler in java.
What i am planning on doing is creating a jumbler that jumbles a word you give it, and since there can be up to thousands of combinations per word, id like for it to randomly return ONE of them whenever you ask to jumble the word.
-
Sort of like the game 'Text Twist' where it jumbles the letters over and over when you click a button.
I believe i have to put these permutations in an array and randomly call one of them out, but i have no idea how to place all of it in an array since i am never sure of how many permutations one word can have.
-
Ok what you have done so far.
-
hhhmmm... interesting...
Maybe something like:
- get word from user
- create an array the size of the word
- start loop
- pick a letter (this can be done in order 1,2,3...)
- generate random number between 1-wordsize
- check to see if there is an element in the random number element
- if yes, get another random number
- if no, place letter in array (random number element)
- stop loop
- print array
There is a catch in the above pseudo code, but it gives an idea how to do it.
Luck,
CJSL
-
Unless you're familiar with algorithms, this is a case where it's usually best to use the library method than inventing something: most people when they try and invent this get it wrong.
So, look at the Collections.shuffle() method. You'll need to pass it a List, such as an ArrayList. So your first task is going to be getting your word (which I assume you have in a String) into an ArrayList. Then, if you're not too fussy, you call Collections.shuffle() on that list.
You'll notice that you can also pass a Random object to Collections.shuffle(). This is because for some uses, the default random number generator is not sufficient. For example, if your word has more than about 16 letters, depending on the number of duplicates, there will be some potentially possible combinations that Collections.shuffle() with the default RNG cannot possibly produce. But for casual use, this "unfairness" doesn't tend to matter too much.
-
neilcoffey, i love you. I didnt know that existed. I just tried it, works perfectly now.
You basically made me convert over 100 lines of failed algorithms into 1..
-
if anyone wants to see it, here it is. Thanks again. :)
Code:
/**
*
* @author Kevin Hohl
*/
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class WordShuffler {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String word;
List listA = new ArrayList();
int yes;
final int SENTINEL = -1;
System.out.println("Enter a word to be shuffled: ");
word = input.nextLine();
word = word.toLowerCase();
for (int i = 0; i < word.length(); i++){
listA.add(word.charAt(i));
}
System.out.println("Jumbled word: " + shuffler(listA));
do {
System.out.println("Shuffle again?(1 to shuffle, -1 to quit)");
yes = input.nextInt();
System.out.println("Jumbled word: " + shuffler(listA));
} while (yes != SENTINEL);
}
public static String shuffler(List a) {
String wrdo = "";
Collections.shuffle(a);
for (int j=0; j < a.size(); j++) {
wrdo += a.get(j);
}
return(wrdo);
}
}
-
Word Jumbler
Here's what I cooked up(went the long way):
Code:
import javax.swing.JOptionPane;
import java.util.Random;
public class WordJumble
{
public static void main (String [ ] args)
{
String wordToJumble = JOptionPane.showInputDialog("What word do you want to jumble?");
char charArray [] = new char [wordToJumble.length()];
Random randomNum = new Random();
char letter;
int randomElement;
int count = 0;
for (int i=0; i<wordToJumble.length();i++)
{
letter = wordToJumble.charAt(i);
randomElement = randomNum.nextInt(wordToJumble.length());
while (count < wordToJumble.length())
{
if (charArray[randomElement] == '\u0000')
{
charArray[randomElement] = letter;
count++;
break;
}
else
{
randomElement = randomNum.nextInt(wordToJumble.length());
}
}
}
String jumbledWord = "";
for (int i = 0; i<charArray.length; i++)
{
jumbledWord = jumbledWord + String.valueOf(charArray[i]);
}
JOptionPane.showMessageDialog(null,jumbledWord);
}
}
CJSL