Results 1 to 5 of 5
  1. #1
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default Scramble a String

    I'm trying to scramble a String and I have it working, I was just wondering if there is a more efficient way of doing it than what I have. Does anyone have a suggestion on how to make this more efficient?
    Java Code:
    String abc = "abcdefghijklmnopqrstuvwxyz";
    String scrambled = "";
    Random randGen = new Random();
    
    while(scrambled.length() != abc.length())
    {
       char rand = abc.charAt(randGen.nextInt(abc.length()));
       if(!scrambled.contains("" + rand))
          scrambled += rand;
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    What is more efficient:
    Fewer machine instructions executed
    or fewer source statements.

    Should the output String contain the same number of letters as the input String?
    Your algorithm appears to only allow one copy of any letter. The longest that your output string could be would be 26 letters for any long string input to it.
    Last edited by Norm; 07-15-2011 at 11:41 PM.

  3. #3
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default

    I have no idea which is more efficient... The output String should contain the exact characters of the input String just mixed up.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    The output String should contain the exact characters of the input String just mixed up.
    Have you tested it with some different input to see what happens?

    I have no idea which is more efficient.
    I was asking you what you meant by being efficient. How do you determine if one piece of code is more efficient than another?

    Which is worth more a one dollar bill or 30 quarters. The one dollar bill is more efficient to carry.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    One thing I can say immediately is to use a stringbuilder instead of string concatenation. Then use append. Norm made some good points as well, this should probably take a string input and scramble it.

Similar Threads

  1. Replies: 1
    Last Post: 11-04-2010, 07:53 PM
  2. String builder scramble word game
    By moncur in forum New To Java
    Replies: 4
    Last Post: 10-22-2010, 04:14 AM
  3. Scramble Java Game
    By equinox55 in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 12:30 AM
  4. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 04:42 AM
  5. Word Scramble
    By lk9865 in forum New To Java
    Replies: 5
    Last Post: 11-17-2007, 03:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •