Results 1 to 8 of 8
Thread: Word Jumbler
- 02-20-2009, 02:19 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
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.
- 02-20-2009, 02:26 AM #2
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
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.
- 02-20-2009, 02:27 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 02-20-2009, 02:29 AM #4
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-20-2009, 02:31 AM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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.Neil Coffey
Javamex - Java tutorials and performance info
- 02-20-2009, 03:10 AM #6
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
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..
- 02-20-2009, 03:28 AM #7
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
if anyone wants to see it, here it is. Thanks again. :)
Java 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); } }Last edited by wethekings; 02-20-2009 at 03:31 AM.
- 02-20-2009, 04:57 AM #8
Word Jumbler
Here's what I cooked up(went the long way):
CJSLJava 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); } }Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Regex that matches whole word
By moaxjlou in forum New To JavaReplies: 13Last Post: 11-04-2008, 05:48 PM -
Word to xml Conversion
By kushagra in forum Advanced JavaReplies: 3Last Post: 10-16-2008, 08:23 AM -
Word Frequency
By capu in forum Advanced JavaReplies: 2Last Post: 10-09-2008, 02:03 PM -
Word OLE
By Java Tip in forum SWTReplies: 0Last Post: 07-25-2008, 02:33 PM -
Word Scramble
By lk9865 in forum New To JavaReplies: 5Last Post: 11-17-2007, 02:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks