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.