Thread: Word Scramble
View Single Post
  #6 (permalink)  
Old 11-17-2007, 03:22 AM
lk9865 lk9865 is offline
Member
 
Join Date: Nov 2007
Posts: 8
Rep Power: 0
lk9865 is on a distinguished road
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

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)+".");
    }
    
}
Reply With Quote