Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Create new String to contain each charector once in Random order

807589Jul 9 2008 — edited Jul 10 2008
Can 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......
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 7 2008
Added on Jul 9 2008
14 comments
332 views