Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-15-2007, 11:15 PM
Member
 
Join Date: Nov 2007
Posts: 8
Rep Power: 0
lk9865 is on a distinguished road
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
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-16-2007, 01:22 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,389
Rep Power: 3
hardwired is on a distinguished road
Default
The trouble is in this code block:
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:
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".
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-16-2007, 05:47 AM
Member
 
Join Date: Nov 2007
Posts: 8
Rep Power: 0
lk9865 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-16-2007, 08:56 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,389
Rep Power: 3
hardwired is on a distinguished road
Default
So, something like this:
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);
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-16-2007, 06:25 PM
Member
 
Join Date: Nov 2007
Posts: 8
Rep Power: 0
lk9865 is on a distinguished road
Default
you are a frigen genius hardwired
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-17-2007, 02:22 AM
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)+".");
    }
    
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
copying file from a email/word to a Java application cmbl Advanced Java 13 01-09-2008 06:51 AM
Help with a word, if it is divided by spaces baltimore New To Java 1 08-07-2007 06:31 AM
How to update Existing Word Doc Using java umashankar Advanced Java 1 07-27-2007 01:29 PM
How to know the exact word on which the mouse is, in a JTextArea JavaBean AWT / Swing 1 05-19-2007 12:03 PM


All times are GMT +2. The time now is 03:58 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org