Results 1 to 15 of 15
- 02-07-2009, 05:48 AM #1
[SOLVED] Help With Word Scrambling Application
I was reading about the thing were if you scramble a word but keep the first and last letters in their correct positions, you are able to read it. I saw a link to a program that scrambled text in this way, so I decided to make my own. Nested for loops hurt my head, I keep forgetting what i'm trying to do, but I finished it and I think its fine. I use Netbeans 6.5 by the way. I tried to comment a bit so you can see what's going on, but I don't understand the error I'm getting. It compiles fine, but if I try running in Netbeans, Nothing happens. When I try in command prompt, I get this Error Message:(I didnt write all the folder names thats what the dots are)
Heres the code.. Please help I don't get why it's not working :confused:Java Code:C:\Users\.............\>java WordMixer Exception in thread "main" java.lang.NoClassDefFoundError: WordMixer (wrong name : jmath/components/WordMixer) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$000(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source)
So why won't it work?? as I was commenting i noticed 2 problems... 1 is not that horrible and probly easy solution: it removes punctuation, and... i know I'l use split method with boolean param that lets delimeters be counted as tokens then at start of word cycle loop i'l put an if .equals then a lot of || and repetition to put all the delims and then inside the if put a continue;. Ok that one solved, but the other problem is that This takes very long probably and because it keeps making random numbers until it's one that hasn't already been used, down to the point where theres one valid number out of the length of the word - 2 possible generated numbers.. if you have solutions to the error causing or the problem tell me please. And thankyou.Java Code:package jmath.components; public class WordScrambler { private static int genRandomInteger(int range) { // range = word length return (int)(Math.random() * (range - 3)) + 1; // makes random number } private static String scrambleWords(String txt) { // txt is the words int[] used; /* the letter locations in the word that have already been used by a different scrambled character */ int location; // int to add to the used int[] when placing a letter boolean already = false; // has the location in the word been taken? char[] temp; // array to store the new scrambled word temporarily int rand = 1; // random number int range = 0; // length of word String[] tokens = txt.split(" ,?!.()/\"\':;"); // the array of words & delims /* **** 1 prob with this program is it removes all punctuation... */ String res = ""; // the returned result for(int i = 0; i < tokens.length; i++) { // this loop cycles the words if(tokens[i].length() <= 3) continue; // 3 or less letter word cant be scrambled location = -1; // always starts off -1 for each word because it gets /* incremented before each time something is added to int[] used so that first is 0, next 1(location in the array) etc. */ range = tokens[i].length(); // the length of word used = new int[range - 3]; // its -3 not -2 incase you thought that /*** if you look down it goes through random positions in the word to scrambl e the letters so a prob it takes a long time probably to get the right random num... */ temp = new char[range]; // temporary array to hold scrambled word temp[0] = tokens[i].charAt(0); // first and last stay same so put temp[range - 1] = tokens[i].charAt(range - 1); // them in right away for(int j = 1; j < range - 1; j++) { // this loop cycles letters while(rand != 0) { // this loop finds valid random position in word rand = genRandomInteger(range); for(int k = 0; k < used.length; k++) { // this sees if # is already used if(rand == used[k]) { already = true; break; } } if(already == true) continue; // then try anther num.. else { used[++location] = rand; // add to used break; // valid position found } } temp[rand] = tokens[i].charAt(j); // put the letter in } // next letter- back to top of loop String s = new String(temp); // put the scambled word in String res += s + " "; // add to returned result } return res; } public static void main(String[] args) { String text = "Can You peoples Read This?"; // I made it short so it would go fast System.out.println(scrambleWords(text)); // print it } }Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-07-2009, 06:49 AM #2
removed package line and it runs... but its an endless loop.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-07-2009, 06:57 AM #3
OH! haha very nice
don't know what your error was, but i changed it from using break to break label and it worked. prob just some logic bug.
Java Code:S:\TMP>java WordScrambler C oT d h n elsY ? S:\TMP>java WordScrambler Cnld p s i R eY a hs T ? S:\TMP>java WordScrambler Ce hsaY Te l d e Roi ? S:\TMP>java WordScrambler Csep p iea l T d ?
Java Code:for(int j = 1; j < range - 1; j++) { // this loop cycles letters [B]endw:[/B] while(rand != 0) { // this loop finds valid random position in word rand = genRandomInteger(range); for(int k = 0; k < used.length; k++) { // this sees if # is already used if(rand == used[k]) { already = true; [B]break endw;[/B] } } //if(already == true) continue; // then try anther num.. //else { used[++location] = rand; // add to used break; // valid position found //} }USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-07-2009, 09:27 AM #4
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
you're trying to run WordMixer, but there is only WordScrambler...
- 02-07-2009, 04:09 PM #5
no i changed the name after.. i might of put the error for the old name and the code for the new name, but i renamed after.
That has nothing to do with error. I tried the break label and it worked.. sort of.. but it got this output:
And the text to be scrambled was this:"Can You peoples Read This?"... how did it get that?Java Code:c id Ts e p a ?
Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-07-2009, 04:15 PM #6
I found the problem... my split delimeters aren't working. It treats the whole text "Can You peoples Read This?" as 1 token.... Why? what did I do wrong? EDIT: Ok i tried making the delimeter just a space, i thought each char in the string of the split method would be a delimeter, but anyway, i made it just a space, and now it counted as 5 words, but the program is in endless loop somehow, not doing anything, still says running... heres the code again so you can see what I've done.
Java Code:package jmath.components; public class WordScrambler { private static int genRandomInteger(int range) { return (int)(Math.random() * (range - 3)) + 1; } private static String scrambleWords(String txt) { int[] used; int location; boolean already = false; char[] temp; int rand = 1; int range = 0; String[] tokens = txt.split(" "); for(int i = 0; i < tokens.length; i++) { System.out.println(tokens[i]); System.out.println(tokens.length); } String res = ""; for(int i = 0; i < tokens.length; i++) { if(tokens[i].length() <= 3) continue; location = -1; range = tokens[i].length(); used = new int[range - 3]; used[0] = 0; temp = new char[range]; temp[0] = tokens[i].charAt(0); temp[range - 1] = tokens[i].charAt(range - 1); for(int j = 1; j < range - 1; j++) { endw: while(rand != 0) { rand = genRandomInteger(range); for(int k = 0; k < used.length; k++) { if(rand == used[k]) { already = true; break endw; } } if(already == true) continue; else { used[++location] = rand; break; } } temp[rand] = tokens[i].charAt(j); } String s = new String(temp); res += s + " "; } return res; } public static void main(String[] args) { String text = "Can You peoples Read This?"; System.out.println(scrambleWords(text)); } }Last edited by MK12; 02-07-2009 at 04:20 PM.
Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-07-2009, 04:20 PM #7
nice call emceenugget, didn't catch that. ;)
only reason why you get error was cauze your packages. but in netbeans you get nothing cauze it was an endless loop. thats fixed.
what is the output that you want? i thought you wanted to scramble the letters. if its the words, why not just put it in a list and shuffle it, then display it.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-07-2009, 04:26 PM #8
Forget the renaming thing, I just decided after that Scrambler sounds better. The endless loop isnt fixed, read my post, When i got it to treat each word as a seperate word (but then i can only use a space as delimeter), it endless loops again. I don't get what you mean with packages causing errors.. The scrambling i wanted was like this:
"Can You peoples Read This?" would be something like this:
"Can You plpoees Raed Tihs?"
Each word scrambled by individually, but keeping the first and last letter in the right place.
As in each word stays in the right place, just letters in between the first and last letters(thats why 3 or less letter words can't get scrambled) get mixed up.
EDIT: Ok even if i make the text 1 word, so that it won't endless loop, for the word "Equivalent" I got:
"En a t " ... it has a bunch of spaces and missing letters.. same as original word, but last letter in wrong spot.. every time the t is in second last position, with a space after it. Somhow alot of the letters don't get assigned... :confused:
EDIT:Ok nevermind i got mixed up itdoes put the first and last letters in their correct positions, but why does it not get all the letters? it missed more then half the letters in the middle and leaves them as whitespace. Here are my priorities:
1. Get the 1 word to work so that all the letters in the middle get placed in the scrambled word.
2. Make it work with lots of words so that it won't go in endless loop when more then 1 word(don't know why it does that)
3. Make the delimeters work, For some reason i have to just put a space, i don't know how to make more then one thing a possible delimeter....actually i think i figured it out.. anyway..
4. Make it more effiecient. now it goes through random numbers until it finds one that isnt used, takes longer.
EDIT: ok cross out #3 im using stringtokenizer instead so delimiters can be counted, punctuation not removed, and it uses ,.!?()-/&:;\" as delimiters not just a space. And delimeters won't be scrambled because they're all 1 character long and it doesnt scramble unless word is 4 or more characters long. But it still endless loops if theres more then 1 token, and if it's 1 token then it doesnt put in all the letters.. I'll show you the code so far, please help.
Java Code:private static String scrambleWords(String txt) { int[] used; int location; boolean already = false; char[] temp; int rand = 1; int range = 0; StringTokenizer st = new StringTokenizer(txt, " ,.!?()-/&:;\"", true); String[] tokens = new String[st.countTokens()]; int l = 0; while(st.hasMoreTokens()) { tokens[l] = st.nextToken(); l++; } for(int i = 0; i < tokens.length; i++) { System.out.println(tokens[i]); } System.out.println(tokens.length); String res = ""; for(int i = 0; i < tokens.length; i++) { if(tokens[i].length() <= 3) continue; location = -1; range = tokens[i].length(); used = new int[range - 2]; used[0] = 0; temp = new char[range]; temp[0] = tokens[i].charAt(0); temp[range - 1] = tokens[i].charAt(range - 1); for(int j = 1; j < range - 1; j++) { endw: while(rand != 0) { rand = genRandomInteger(range); for(int k = 0; k < used.length; k++) { if(rand == used[k]) { already = true; break endw; } } if(already == true) continue; else { used[++location] = rand; break; } } temp[rand] = tokens[i].charAt(j); } String s = new String(temp); res += s + " "; } return res; } public static void main(String[] args) { String text = "Equivalent.. something? \"this\""; System.out.println(scrambleWords(text)); }Last edited by MK12; 02-07-2009 at 06:04 PM.
Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-07-2009, 06:14 PM #9
if(tokens[i].length() <= 3) continue;
which means any 3 char word won't get added to res.
i'll take a look at ur new code now...USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-07-2009, 06:26 PM #10
oh thanks for poiting that out.. i forgot to make it add to the result.. but still that hasn't been causing aany problems.. i'll go change it now.. thanks for looking at it too. In the code i removed the addidng space from
because now whitespace(and all delimiters) are counted as tokens.Java Code:res += s + " "; I changed it to res += s;
And now I changed this:
Java Code:if(tokens[i].length() <= 3) { continue; } to this: if(tokens[i].length() <= 3) { res += tokens[i]; continue; }Last edited by MK12; 02-07-2009 at 06:30 PM.
Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-07-2009, 07:32 PM #11
Ok I stared at the loops for a while and changed some things that didn't make sense before. Also made stuff not static. But now it endless loops even with just 1 word.. Here is the code so far, first though here is what I need help on(and help in order, I can't do number 3 until 1 and 2 are done for example):
1. Make 1 word work (scramble correctly - scramble letters inside word but keep first and last letters in the right spots e.g. Parentheses -> Ptehesnares, goal -> gaol, etc.)
2. Make it work with a lot of tokens, get rid of endless loop(hopefully solving #1 will also solve this one)
3. More efficient, right now it makes random numbers, word length - 2 different possibilities , down to the point where only one of the possibilities will be accepted.
4. uhm.. Make it Scramble text from a .txt document.
Heres Code.
If you want comments look at the first post. If it's too different ask me I'll comment this one, ... yeah.Java Code:private int genRandomInteger(int range) { return (int)(Math.random() * (range - 3)) + 1; } private String scrambleWords(String txt) { int[] used; int location; boolean already = false; char[] temp; int rand = 1; int range = 0; StringTokenizer st = new StringTokenizer(txt, " ,.!?()-/&:;\"", true); String[] tokens = new String[st.countTokens()]; int l = 0; while(st.hasMoreTokens()) { tokens[l] = st.nextToken(); l++; } for(int i = 0; i < tokens.length; i++) { System.out.print(tokens[i]); } System.out.println(tokens.length); String res = ""; for(int i = 0; i < tokens.length; i++) { if(tokens[i].length() <= 3) { res += tokens[i]; continue; } location = -1; range = tokens[i].length(); used = new int[range - 2]; used[0] = 0; temp = new char[range]; temp[0] = tokens[i].charAt(0); temp[range - 1] = tokens[i].charAt(range - 1); for(int j = 1; j < range - 1; j++) { int times = 0; endw: while(true) { rand = genRandomInteger(range); for(int k = 0; k < used.length; k++) { times ++; if(rand == used[k]) { already = true; break; } if(times == used.length) { break; } } if(already == false) { used[++location] = rand; break endw; } } temp[rand] = tokens[i].charAt(j); } String s = new String(temp); res += s; } return res; } public static void main(String[] args) { String text = "Equivalent"; WordScrambler ws = new WordScrambler(); System.out.println(ws.scrambleWords(text)); }Last edited by MK12; 02-07-2009 at 07:35 PM.
Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-07-2009, 08:08 PM #12
you still trying to fix the endlessssssss loop? LOL
I've already gaved up on it and re-wrote the loop using arraylist.
Java Code:ArrayList<Character> c = new ArrayList<Character>(); for(int j = 1; j < range - 1; j++) { c.add(tokens[i].charAt(j)); } Collections.shuffle(c); Character[] cc = c.toArray(new Character[0]); String z = new String(); for(Character x:cc){ z += x.toString(); } String s = new String(temp[0] + z + temp[range-1]);Java Code:Hlleo Wldro! Hlelo Wdlro! Can You plpoee Raed Tish? Can You poelpe Raed This?
Last edited by angryboy; 02-07-2009 at 08:27 PM.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-07-2009, 08:24 PM #13
does that replace all 4 nested loops?
nvm...
Thanks a lot. That made it so much easier.Last edited by MK12; 02-07-2009 at 08:31 PM.
Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-07-2009, 08:25 PM #14
- 02-07-2009, 09:26 PM #15
Similar Threads
-
Word to xml Conversion
By kushagra in forum Advanced JavaReplies: 3Last Post: 10-16-2008, 08:23 AM -
Word OLE
By Java Tip in forum SWTReplies: 0Last Post: 07-25-2008, 02:33 PM -
Scrambling Words
By Shadow22202 in forum New To JavaReplies: 9Last Post: 04-30-2008, 03:51 AM -
copying file from a email/word to a Java application
By cmbl in forum Advanced JavaReplies: 13Last Post: 01-09-2008, 06:51 AM -
Word Scramble
By lk9865 in forum New To JavaReplies: 5Last Post: 11-17-2007, 02:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks