aspose file tools*
The moose likes Java in General and the fly likes Shuffle a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Shuffle a string" Watch "Shuffle a string" New topic
Author

Shuffle a string

Alex Mindricel
Greenhorn

Joined: Apr 24, 2011
Posts: 14
Can you please tell me how can I shuffle a string?Actualy i want to shufle a string that was splited with tokenizer?Like in a sentence i want to split it into words and after that shuffle each words,and after that reattach the shuffeld words into a sentence.Please give me an example.
Thanks!
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Check out String.split and Math.random and String.concat
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

Hi Alex. Welcome to The Ranch!

The Collections class has got a built-in shuffle method, that can act on any List. So one approach to the shuffling part of the problem would be to convert each word into a List of characters, and hit it with that. Any use?
Alex Mindricel
Greenhorn

Joined: Apr 24, 2011
Posts: 14
Thank you.
So I need to split the sentence into words(wich one is better,considering the fact that i want white spaces as a delimiter,String buffer,or string tokenizer?) and after that I need to convert each word into list characters,right? Could you please explain the last the part with a short example?(just to clear things up).Thank you very much.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

Well, from the StringTokenizer documentation:
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

So I'd suggest you're better off with String.split().

To do the shuffling, I'd probably do the following (though there are other ways as well):
- Create an ArrayList<Character>
- Iterate over the String (look for a method that gives you access to characters), and add each one to the ArrayList.
- Shuffle the list with Collections.shuffle
- Convert back to a String by iterating through the ArrayList, appending each value to a StringBuilder.

It's a little less elegant than I'd like, and you could avoid the trouble of converting to and from a List by shuffling the String in place, but then you'd have to write your own shuffle method (I think). If you look at the documentation for Collections.shuffle it describes how the shuffling algorithm works if you wanted that as a starting point to write your own.
Alex Mindricel
Greenhorn

Joined: Apr 24, 2011
Posts: 14
So,something like this:

This is for a single word,but i'm not sure it will work,and the other thing is about the word1,word2 strings.How can i create a loop to run until there are no more words.(assuming that the code is correct).
Thanks a bunch!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

Don't worry about the multiple words yet. I suggest you get the shuffling right first. Write a method that takes a word as input, shuffles it, and returns the shuffled word. Once you've got that working, then write another method that takes the sentence as input, splits it up into words, and calls your shuffle method on each word.

At the moment it's not quite clear what you're doing, because you're making reference to variables that aren't defined in that method (what are all and one?). I'd also suggest that your shuffling method shouldn't be referring to graphical components like it seems to be. Just shuffle the word and return it - let the calling code decide where the input and output should come from/go to.

So, something like (not fully checked):

Your code suggests you also want to keep the first and last characters unchanged. So you can modify the code to do that...but remember that you might get a single letter word!




Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

It sounds like OP wants to split a sentence into words, rather than shuffling the words. I'd do it like this:

Or a more concise but dirtier version if your sentence doesn't contain commas or brackets
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

Luigi Plinge wrote:It sounds like OP wants to split a sentence into words, rather than shuffling the words. I'd do it like this:

I read it as meaning split a sentence into words, and then separately shuffle each word. If that's not correct, apologies!
Alex Mindricel
Greenhorn

Joined: Apr 24, 2011
Posts: 14
Thanks you guys.The end result is to make a little program that,when a button is pressed,it takes a text form a text field that the user is entering and change it like this:
The first and last char for every word is kept the same,and the other ones randomly mixes,and I was thinking(like noob that i am) to split the enter sentence into words,assign each word to a variable,and after that shuffle each word at
a time.After i'm done shuffeling the words,i have to reatach them,and print them in a text View(like setText).About the all and one variable,they are the sam
What do you think?Beacuse i'm really stuck.
p.s:I made a mistake,the code is something like this,for a single word(without the split string part)

Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

What do I think? I think that should be really easy given the methods above! Go on, you can do it!
Alex Mindricel
Greenhorn

Joined: Apr 24, 2011
Posts: 14
Ok,so the above method shuffels one word,(and kepes the first and the last letter in the same position),but i'm having trouble with the whole sentnce(with the split part).Could you please help me?I want to split the sentce(with string buffer and split string,but what next?How do i assign each word to the method above.Thank you.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Change your method so that it doesn't do anything else except process 1 word, and returns the processed word as a String. Then take the method I gave you and call your method from that, in the bit where each word is being added to the final String.
Alex Mindricel
Greenhorn

Joined: Apr 24, 2011
Posts: 14
So it has to be something like this?
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

You've got the right idea. You might like to make it static because it doesn't depend on object state, and you can test it out to check it's working with a main method, containing a few tests like
Alex Mindricel
Greenhorn

Joined: Apr 24, 2011
Posts: 14
And now i have to create a another method to split the string and atach every shuffeld word?And after that,to print the final result in a textview(it's android and i need to use somethig like mix.setText();.The question is do i need to create a third method for the printing part?
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Yeah, you will call your shuffle method from the actionPerformed method of the ActionListener on your button. You don't *have* to have separate methods for shuffling the words and the sentence, and you could even just stick the whole lot into the actionPerformed method. It's just easier to test and re-use if you do.
Alex Mindricel
Greenhorn

Joined: Apr 24, 2011
Posts: 14
I've called my method from the ActionListener like this:

and now,for the shuffle method i need to do the same thing?I mean like this?

Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Assuming your MixMe method works (BTW you should start method names with lowercase e.g. mixMe, and classes / interfaces with uppercase), you want to call it on each word.

Here is a hint, from the code I gave you above:

Alex Mindricel
Greenhorn

Joined: Apr 24, 2011
Posts: 14
So,something like this?

Assuming that what I wrote above is corect,how will i print the final result(I mean with the setText())?
Alex Mindricel
Greenhorn

Joined: Apr 24, 2011
Posts: 14
I'm not sure if i'm mistaking,but in your method the words shuffle as well?Beacuse i want only the letters to do that.Thank you very much.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441



So now you have a method that TAKES A STRING AS AN ARGUMENT (hint) and RETURNS A PROCESSED STRING (hint). You're on your own now!!
Alex Mindricel
Greenhorn

Joined: Apr 24, 2011
Posts: 14
Thank you so much for all your help!Hope I can manage to print the end result.I guess is something like mix.setText(w).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shuffle a string