Results 1 to 8 of 8

Thread: Word Jumbler

  1. #1
    wethekings is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default 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.

  2. #2
    wethekings is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    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.

  6. #6
    wethekings is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

    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..

  7. #7
    wethekings is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

    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.

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Word Jumbler

    Here's what I cooked up(went the long way):
    Java 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Regex that matches whole word
    By moaxjlou in forum New To Java
    Replies: 13
    Last Post: 11-04-2008, 05:48 PM
  2. Word to xml Conversion
    By kushagra in forum Advanced Java
    Replies: 3
    Last Post: 10-16-2008, 08:23 AM
  3. Word Frequency
    By capu in forum Advanced Java
    Replies: 2
    Last Post: 10-09-2008, 02:03 PM
  4. Word OLE
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:33 PM
  5. Word Scramble
    By lk9865 in forum New To Java
    Replies: 5
    Last Post: 11-17-2007, 02:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •