Results 1 to 6 of 6

Thread: Word Scramble

  1. #1
    lk9865 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default Word Scramble

    Guys I need help with this. Its a project that I am doing. I dont want you to give me or fix the code. I want to be able to do that. But I just need someone to tell me what I am doing wrong. I have been staring at it for the last 3 hours and my brain has reached its threshold limit.

    Ok the objectives of this project is to take a string of words with spaces in between and then for each word keep the first and the last character but scramble whatever is in the middle so here is my...please don't laugh i am a novice at coding especially in java
    Java Code:
    /**
     *
     * @author Lawrence
     */
    import java.util.*;
    public class scramble {
        
        
        
        public static void main(String[] args) {
            
            int randomPos, start=0;
            
            char first, last;
         /*   String text = "This is a pretty simple paragraph. The season is autumn" +
                          "and the air is crisp and clear. The leaves are turning a "+
                          "variety of colors and then sadly drop to the ground";
          */
            String text = "What person willing interesting";
            Random generator = new Random();
            
            for (int i=0; i<text.length(); i++){
                int whiteSpace = text.indexOf(" ", start);
                if (whiteSpace<0){break;} else{
                    
                    first = text.charAt(start);
                    last = text.charAt(whiteSpace-1);
                    
                    System.out.print(first);
    // this is the first character for each word. need the last and scramble the middle
                    
                    String eachWord = text.substring(start+1 ,  whiteSpace-1) ;
                    //this will give me the substring to scramble, need a integer to randomize?
                    
                    int b = (whiteSpace-1)-(start+1);
                    // this gives how many numbers to scramble
                    
                    for (int n=0; n < b; n++){
                        randomPos = generator.nextInt(b); //outputs a random beween 0&b)
                        char a = eachWord.charAt(randomPos); //takes the character at pos
                        System.out.print(a);
                        // onces i choose that racndom character i need to take it out of the list.
                        if((start+1)-randomPos <=1){
                            eachWord = text.substring(randomPos , whiteSpace-1);} 
                        else if((whiteSpace-1)-randomPos <=1){
                            eachWord = text.substring(start+1 , randomPos); }
                        else {
                        eachWord = text.substring(start+1 , randomPos)+ text.substring(randomPos , whiteSpace-1);}    
                    }
                     
                    
                    
                    System.out.println(last+ " "+ eachWord) ;//this is the last charater of each word!
                    
                    
                    
                    start = whiteSpace+1;
                }
                
                
                
                
            }
            
        }
        
    }
    Thank you in advanced

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The trouble is in this code block:
    Java Code:
    if((start+1) - randomPos <= 1) {
        eachWord = text.substring(randomPos , whiteSpace-1);
    } else if((whiteSpace-1) - randomPos <= 1) {
        eachWord = text.substring(start+1 , randomPos);
    } else {
        eachWord = text.substring(start+1 , randomPos) +
                   text.substring(randomPos , whiteSpace-1);
    }
    Trying to penetrate this makes my head hurt.
    I replaced it with this:
    Java Code:
    String scrambled = scramble(eachWord);
    String scrambledWord = String.valueOf(first) + scrambled + last;
    System.out.printf("scrambled = %s  scrambledWord = %s%n",
                       scrambled, scrambledWord);
    and it works okay so far. The scramble method calls another helper method. Breaking
    things up like this makes everything easier to follow, understand and to troubleshoot. It
    is an easy way to break down complex tasks into easy–to–manage steps. Your code block
    above will have to deal with much more complexity to work properly. And since you want to
    do this on your own I'll only say this: I found it much easier to build up a new String by
    appending randomly–selected chars from "eachWord".

  3. #3
    lk9865 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    thank you hardwired,
    you see the algorithm that I am looking at is this. lets say the word "Present"
    if you take the first and the last one out then we have the substring "resen"
    ok then imagine the randomomizer picks from the new substring lets say it picks 3 as illustrated
    [0][1][2][3][4]
    [r][e][s][e][n]
    then the next step would be to take out the 3rd character and the newly word would as such
    [0][1][2][3]
    [r][e][s][n]
    till there is no more substring to randomly pick from.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    So, something like this:
    Java Code:
    System.out.printf("s = %s%n", s);
    while(s.length() > 0) {
        int pos = generator.nextInt(s.length());
        if(pos == 0)
            s = s.substring(1);
        else if(pos == s.length()-1)
            s = s.substring(0, pos);
        else
            s = s.substring(0, pos) + s.substring(pos+1, s.length()-1);
        System.out.printf("pos = %d  s = %s%n", pos, s);
    }
    System.out.println("s = " + s);

  5. #5
    lk9865 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    you are a frigen genius hardwired

  6. #6
    lk9865 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default Finally finished

    Finally finished the program.
    Thanks to hardwired for the guiding to the right direction. Though I had to tweak it a little bit but definitely worked for me.
    So yea if you are bored one and and just want to scramble your boy/girl friends essay without having to manually altering the position the letter then i guess you can use this :rolleyes:

    Java Code:
    /**
     * @author Lawrence
     */
    import java.util.*;
    public class scramble {
        
        
        public static void main(String[] args) {
            
            int pos, start=0;
            
            char first, last;
            String text = "This is a pretty simple paragraph. The season is autumn" +
                 "and the air is crisp and clear. The leaves are turning a "+
                 "variety of colors and then sadly drop to the ground.";
            
            Random generator = new Random();
            
            while (text.length()>0){
                int whiteSpace = text.indexOf(" ", start);
                if (whiteSpace<0){break;} else{
                    
                    first = text.charAt(start);
                    last = text.charAt(whiteSpace-1);
                    
                    if ((whiteSpace-1)-start <=0)
                        System.out.print(text.charAt(start)+ " ");
                    else {
                        String eachWord = text.substring(start+1 ,  whiteSpace-1) ;
                        //this will give me the substring to scramble, need a integer to randomize?
                        
                        System.out.print(first);
    // this is the first character for each word. need the last and scramble the middle
                        
                        while(eachWord.length()>0){
                            pos = generator.nextInt(eachWord.length()); //outputs a random value of the size
                            System.out.print(eachWord.charAt(pos));
                            // onces i choose that racndom character i need to take it out of the list.
                            if (pos==0)
                                eachWord = eachWord.substring(1);
                            else if (pos == eachWord.length()-1 )
                                eachWord = eachWord.substring(0,pos);
                            else
                                eachWord = eachWord.substring(0,pos) + eachWord.substring(pos+1, eachWord.length());
                        }
                        System.out.print(last + " ") ;//this is the last charater of each word!
                    }
                    start = whiteSpace+1;
                    
                }}
            
           int lastWhiteSpace =  text.lastIndexOf(" ");
            System.out.print(text.charAt(lastWhiteSpace+1));
            String lastWord = text.substring(lastWhiteSpace+2 , text.length()-2);
            while (lastWord.length()>0){
                int lpos = generator.nextInt(lastWord.length());
                System.out.print(lastWord.charAt(lpos));
                if (lpos==0)
                    lastWord = lastWord.substring(1);
                else if (lpos==lastWord.length()-1)
                    lastWord = lastWord.substring(0,lpos);
                else
                    lastWord = lastWord.substring(0,lpos)+lastWord.substring(lpos+1,lastWord.length());
            }
        System.out.print(text.charAt(text.length()-2)+".");
        }
        
    }

Similar Threads

  1. Replies: 13
    Last Post: 01-09-2008, 06:51 AM
  2. Help with a word, if it is divided by spaces
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:31 AM
  3. How to update Existing Word Doc Using java
    By umashankar in forum Advanced Java
    Replies: 1
    Last Post: 07-27-2007, 01:29 PM
  4. Replies: 1
    Last Post: 05-19-2007, 12:03 PM

Posting Permissions

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