Results 1 to 6 of 6
Thread: Word Scramble
- 11-15-2007, 11:15 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
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
Thank you in advancedJava 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; } } } }
- 11-16-2007, 01:22 AM #2
The trouble is in this code block:
Trying to penetrate this makes my head hurt.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); }
I replaced it with this:
and it works okay so far. The scramble method calls another helper method. BreakingJava Code:String scrambled = scramble(eachWord); String scrambledWord = String.valueOf(first) + scrambled + last; System.out.printf("scrambled = %s scrambledWord = %s%n", scrambled, scrambledWord);
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".
- 11-16-2007, 05:47 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
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.
- 11-16-2007, 08:56 AM #4
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);
- 11-16-2007, 06:25 PM #5
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
you are a frigen genius hardwired
- 11-17-2007, 02:22 AM #6
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
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
-
copying file from a email/word to a Java application
By cmbl in forum Advanced JavaReplies: 13Last Post: 01-09-2008, 06:51 AM -
Help with a word, if it is divided by spaces
By baltimore in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:31 AM -
How to update Existing Word Doc Using java
By umashankar in forum Advanced JavaReplies: 1Last Post: 07-27-2007, 01:29 PM -
How to know the exact word on which the mouse is, in a JTextArea
By JavaBean in forum AWT / SwingReplies: 1Last Post: 05-19-2007, 12:03 PM


LinkBack URL
About LinkBacks

Bookmarks