Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Jumble

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Angry Jumble

    String jumble(String): accepts a string and returns a jumbled version of the original: for this method, jumbled
    means that two randomly chosen characters other than the first and last characters of the string are swapped; this method
    must use the class, Random. The method must swap two different characters: in other words the two random
    indices into the string cannot be equal, cannot be 0, and cannot be equal to the string’s length minus one. So, for example, a
    four-letter string MUST result in the returned string having the same first and last characters and have the second and third
    characters swapped. Examples of what this method might do: “fist” returns “fsit”, “much” returns “mcuh”, but for longer
    strings there will be more possible return values: “spill” could return “splil” or “sipll”. Only ONE pair of letters should be
    swapped and strings shorter than four characters are returned unchanged. When the string length is greater than 3, the
    original string must never be returned.

    I cant seem to get the first and last character to stay the same. It will always scramble the whole word.
    DO NOT USE ANY CLASSES only random.

    Im a BEGINNER so keep it Easy talk no Array's, appends, etc.

    Use substrings, CharAt, length only. if and else for loops are fine while loops are also okay aswell as do/while.

    public class Test {
    public static void main(String[] args) {
    String s = "goal";
    System.out.print(jumble(s));
    }
    public static String jumble(String s) {
    int i = 0;
    int b = s.length() -1;
    String result = "";
    java.util.Random r;
    r = new java.util.Random();
    char beginning = s.charAt(0);
    char ending = s.charAt(s.length() - 1);
    if ( r.nextBoolean() ) {
    result = beginning + result + s.substring(1, b) + ending;
    }
    else{
    result += beginning + s.substring(1, b)+ ending;
    }
    return result;
    }
    }
    Last edited by kram; November 1st, 2011 at 08:57 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Jumble

    Why are you using a for loop? Since the task only requires you to swap 2 chars then I would generate 2 random numbers. If those numbers are greater than 0, less than length - 1 and not equal to each other then swap the 2 chars at those locations.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Jumble

    Wouldn't i need the for loop because it would grab the specific character from the string.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Jumble

    No you don't need a for loop.
    String text = "abcd";
    int index1 = 1; //change to be a random number
    int index2 = 2; //change to be a random number
    StringBuilder jumble = new StringBuilder();
    jumble.append(text.charAt(0));
    jumble.append(text.charAt(index2));
    jumble.append(text.charAt(index1));
    jumble.append(text.charAt(text.length() - 1));
    System.out.println(jumble);
    Improving the world one idiot at a time!

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Jumble

    I should add that you would need to use substring to allow the code to be more flexible and handle Strings of any length.
    Improving the world one idiot at a time!

Tags for this Thread