Sometimes you need to scramble a String in Java - perhaps you're writing an anagram programme. You can do this by treating the input string as a List of String. The code file is HERE
import java.util.*;
public class Scramble {
public static void main(String[] args) {
System.out.printf("'%s'\n", Scramble.getScrambled(args[0]));
}
public static String getScrambled(String s) {
String[] scram = s.split("");
List<String> letters = Arrays.asList(scram);
Collections.shuffle(letters);
StringBuilder sb = new StringBuilder(s.length());
for (String c : letters) {
sb.append(c);
}
return sb.toString();
}
}
public class Scramble {
public static void main(String[] args) {
System.out.printf("'%s'\n", Scramble.getScrambled(args[0]));
}
public static String getScrambled(String s) {
String[] scram = s.split("");
List<String> letters = Arrays.asList(scram);
Collections.shuffle(letters);
StringBuilder sb = new StringBuilder(s.length());
for (String c : letters) {
sb.append(c);
}
return sb.toString();
}
}