9 Replies Latest reply: Jan 8, 2008 12:33 PM by 807603 RSS

    shuffle characters in String

    807603
      Hy,I'll start with a program i'm dealing with.
      I need to shuffle the characters in a String besides the first and last character in some text file for example.
      Well i started like this
      i put the word in some String
      then i make char[] c = s.toCharArray();
      then make a list.
      List l = Arrays.asList(c);
      Collections.shuffle(l);
      Of course this doesn't work,i suppose because the list can't take char array,but in doc it is Object[]..My question is:if it is there anyway to shuffle the characters of a String with shuffle method in Collections?
        • 1. Re: shuffle characters in String
          807603
          Don't forget that you have the class Character, which is an object, and wraps characters.......
          • 2. Re: shuffle characters in String
            807603
            Ok,Thanks!
            How to convert from char array to Character array?
            • 3. Re: shuffle characters in String
              807603
              Using a
              for
              loop?
              • 4. Re: shuffle characters in String
                807603
                Yes i don't know any other way then to use the loop.Maybe there is some method that i don't know of?
                • 5. Re: shuffle characters in String
                  807603
                  The_Winner22 wrote:
                  Yes i don't know any other way then to use the loop.Maybe there is some method that i don't know of?
                  Yep. The one you're about to write :-)
                  • 6. Re: shuffle characters in String
                    807603
                    i wroted the program like this:

                    import java.util.*;

                    public class TextModifier{
                              public static void main(String[] args){
                                        StringTokenizer str = new StringTokenizer("Filip je all the best.Love him."," .");
                                        String s;
                                        String output = "";
                                        while(str.hasMoreTokens()){
                                                  s = str.nextToken();
                                                  if(s.length() > 3){
                                                            char[] c = s.toCharArray();
                                                            Character[] ch = new Character[c.length];
                                                            for(int i = 0;i < c.length;i++)
                                                                      ch[i] = new Character(c);
                                                            List l = Arrays.asList(ch);
                                                            Collections.shuffle(l);
                                                  }
                                                  for(Iterator ite = l.iterator();ite.hasNext();){
                                                            output += (String)ite.next()+" ";
                                                  }
                                        }
                                        System.out.println(output);
                              }
                    }

                    I hope you'll figure out what i wroted ,when i paste it paste like this.:)
                    Well comiler puts me this:

                    C:\java>javac TextModifier.java
                    TextModifier.java:18: cannot find symbol
                    symbol : variable l
                    location: class TextModifier
                    for(Iterator ite = l.iterator();
                    ^
                    ite.hasNext();){

                    1 error

                    Why?
                    • 7. Re: shuffle characters in String
                      807603
                      TextModifier.java:18: cannot find symbol
                      symbol : variable l
                      location: class TextModifier
                      At line 18 of TextModifier.java the variable 'l' cannot be found. You've definied it out of the scope. Define it inside the scope of the line 18.

                      Selection and iteration statements have its own scope, so declare 'l' outside the if () statement so that it is accessible in the subsequent for () statement.
                      • 8. Re: shuffle characters in String
                        807603
                        Oh, oh, oh, to have a join:
                        String s = "hello, world!";
                        List<String> list = Arrays.asList(s.split(""));
                        Collections.shuffle(list);
                        String result = join(list);
                        System.out.println(result);
                        • 9. Re: shuffle characters in String
                          807603
                          BigDaddyLoveHandles wrote:
                          Oh, oh, oh, to have a join:
                          Et voil´┐Ż:
                              /**
                               * Join the given collection with the given join value.
                               * @param collection The collection (List, Set) to be joined.
                               * @param join The value to be joined between each part.
                               * @return The joined collection.
                               */
                              public static String join(Collection<?> collection, String join) {
                                  StringBuilder builder = new StringBuilder();
                          
                                  for (Iterator<?> iter = collection.iterator(); iter.hasNext();) {
                                      builder.append(iter.next());
                          
                                      if (iter.hasNext()) {
                                          builder.append(join);
                                      }
                                  }
                          
                                  return builder.toString();
                              }