Create new String to contain each charector once in Random order
807589Jul 9 2008 — edited Jul 10 2008Can some body please help me ??? I am stuck......... I am trying to create a new string that whould have the same length and will contain each character of the original string once (str in the code below) in a random order. The following code is generating a string with random characters from the original string with the same length but contains character repeating more than once from the original string. How do I create new string without repaeting the characters more than once.....?????????
public class TestClass {
public static void main(String[] args) {
String str="This is a test class";
char[] char1 = str.toCharArray();
StringBuffer sb=new StringBuffer();
Random r = new Random();
for(int i=1;i<=char1.length;i++){
sb.append(str.charAt(r.nextInt(char1.length)));
}
System.out.print("New Randomly generated String :" sb.toString());
}
}
Thanks in advance......