Jump to content

Word Scrambler Program Help

- - - - -

  • Please log in to reply
4 replies to this topic

#1
karch1992

karch1992

    Newbie

  • Members
  • Pip
  • 2 posts
I have to make a code that scrambles a word but leaves the first and last character in place. I think I have most of it correct, I just can't figure out how to randomly generate a number (representing a letter in the string) and then formatting it into a substring. Here is what I have:

import java.util.Random;

import java.util.Scanner;

public class SentenceScrambler {

	public static void main(String[] args){

		do{

		        String word = readWord();

			int wordLength = scrambleWord(word);

		    }

	while (readWord().equalsIgnoreCase("Stop"));

		     }


	public static String readWord(){

		

		Scanner in = new Scanner(System.in);

		System.out.print("Please enter your word: ");

		String word = in.next();

		return word;

	}

	/**

	 * Finds the beginning of the word

	 * @param word 

	 * @return 

	 */

	public static int scrambleWord (String word){

		int counter = 0;

		while (counter < word.length())

		{

			counter++;

		}

		int firstChar = 0;

		int lastChar = counter;

		int wordLength = counter - 1;

		Random rn = new Random();

		

		int counter2 = 0;

		while (counter2 < wordLength)

		{

		int randomNumbers = rn.nextInt(wordLength-2)+1;

		counter2++;

		

		}

	}

		

		

		

		

	public static void printWord(String scrambledWord)

	{

		System.out.print(newWord);

	}}


Thanks to whoever can help me figure this out.

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
What I would do is chop off the first and last letter first, so all you have is a string of the letters to scramble, mix those up, and then stick the first and last letters back on. Like this:

String lettersToScramble = word.substring(1, word.length() - 2);

char firstLetter = word.charAt(0);

char lastLetter = word.charAt(word.length() - 1);


String scrambledLetters = "";

Random rn = new Random();


while (lettersToScramble.length() > 0) {

    // Pick a letter at random from lettersToScramble

    int i = rn.nextInt(lettersToScramble.length());

    char randomLetter = lettersToScramble.charAt(i);


    // Add the random letter to the new string

    scrambledLetters += randomLetter;


    // Remove the character from the remaining lettersToScramble

    String firstHalf;

    String secondHalf;

    try {

        firstHalf = lettersToScramble.substring(0, i - 1);

    } catch (IndexOutOfBoundsException ex) {

        firstHalf = "";

    }

    try {

        secondHalf = lettersToScramble.substring(i + 1);

    } catch (IndexOutOfBoundsException ex) {

        secondHalf = "";

    }

    lettersToScramble = firstHalf + secondHalf;

}


// scrambledLetters should now contain all the letters from lettersToScramble, with their order scrambled.

String scrambledWord = firstLetter + scrambledLetters + lastLetter;


Edited by gregwarner, 28 March 2011 - 08:41 PM.

Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
karch1992

karch1992

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks, that looks good, but what does 'c' represent here? Its not an initialized variable in the code.

 // Add the random letter to the new string

    scrambledLetters += c;



#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Crap, that was supposed to be "randomLetter" from the line above it. I must've lost my train of thought when I was naming variables. I edited my previous post to make it correct.

Sometimes I think my mind is going the older I get. :)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5
margusmartsepp

margusmartsepp

    Newbie

  • Members
  • Pip
  • 2 posts
If I was you, I would not use Random at all.

	public static String contentSuffle(String word) {

		StringBuilder sb = new StringBuilder();

		int max = word.length() - 1;

		ArrayList<Character> content = new ArrayList<Character>();


		for (Character c : word.substring(1, max).toCharArray())

			content.add(c);

		Collections.shuffle(content);

		for (Character c : content)

			sb.append(c);


		return sb.insert(0, word.charAt(0)).append(word.charAt(max)).toString();

	}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users