1 Reply Latest reply on Nov 15, 2006 4:10 AM by 807607

    A freshman asking help about string

      I am a freshman in Java programing. I have a problem about string in Java programing.

      Could you let me know how to randomly generate a string? For example, when I input a string "abcde", then the output could be "acdeb", or "bdace", and so on. In one word, the output should be a random string which are formed by the five inputed characters. Thank you very much!
        • 1. Re: A freshman asking help about string
          There are "standard" ways of tackling this - including the one used in Sun's
          Collections.shuffle() method. But I guess the value of this exercise lies in
          working out some algorithm of your own.

          It might help to know that you can convert a string into an array of char values.
          String test = "abcde";
          char[] arr = test.toCharArray();
          This creates an array consisting of the the chars 'a', 'b', 'c', 'd', 'e'.

          Before you can use this in any meaningful way you need to imagine the
          letters in front of you. It might help to imagine the letters each being in a
          numbered position (like they are in an array). Think, and write down, how
          you would go about making a random word using all the letters. You can
          assume you have dice or some other source of random numbers available.

          Once you have something definite in plain English (or whatever language
          you're most comfortable in), then have a go at writing the code in Java.