• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Shuffle a string

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out String.split and Math.random and String.concat
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do I think? I think that should be really easy given the methods above! Go on, you can do it!
 
Alex Mindricel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it has to be something like this?
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
yeah, but ... what would PIE do? Especially concerning this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic