Results 1 to 6 of 6
Thread: Jumble Multiple Words
- 02-20-2009, 06:35 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
Jumble Multiple Words
Hi, I've kinda hit a snag at this.
Ive written my code to jumble a word, but i need it to be able to jumble multiple words, without mixing them together.
What i mean is like say i input: "readers digest"
i would like it to produce something like: "esearrd tegsdi"
instead of what it now produces as something like "reedraetssi dg"
any help with this would be very much appreciated, thank you :)Java Code:/** * * @author Kevin Hohl */ import java.util.List; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class WordShuffler { public static void main(String[] args) { Scanner input = new Scanner(System.in); String word; List listA = new ArrayList(); int yes; final int SENTINEL = -1; System.out.println("Enter a word to be shuffled: "); word = input.nextLine(); word = word.toLowerCase(); for (int i = 0; i < word.length(); i++){ listA.add(word.charAt(i)); } System.out.println("Jumbled word: " + shuffler(listA)); do { System.out.println("Shuffle again?(1 to shuffle, -1 to quit)"); yes = input.nextInt(); System.out.println("Jumbled word: " + shuffler(listA)); } while (yes != SENTINEL); } public static String shuffler(List a) { String wrdo = ""; Collections.shuffle(a); for (int j=0; j < a.size(); j++) { wrdo += a.get(j); } return(wrdo); } }
- 02-20-2009, 07:39 AM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
if you're familiar with regexes, you can use the split() method of the String class. if not, you can try out the StringTokenizer class.
- 02-20-2009, 08:20 AM #3
i suggest breaking you original string into string arrays. and break down your class to use methods.
Java Code:public class MultiWordShuffler { public static void main(String[] args){ String s = "Reader Digest"; String[] stringArray = ... // split s into arrays. for(int i=0; ... ){ stringArray[i] = wordShuffler(stringArray[i]); } // combine stringArray back into one string. } public static String wordShuffler(String s){ } public static String shuffler(List a){ } }USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-20-2009, 09:34 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
Woo finish
Thanks. I finally figured out how to do it.
Last edited by wethekings; 02-20-2009 at 09:51 AM.
- 02-20-2009, 02:22 PM #5
Please go to the top of the page, then click Thread Tools --> Mark This Thread As Solved.
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-20-2009, 03:57 PM #6
Similar Threads
-
Money to Words
By javanewbie in forum New To JavaReplies: 2Last Post: 06-30-2008, 04:27 AM -
Scrambling Words
By Shadow22202 in forum New To JavaReplies: 9Last Post: 04-30-2008, 03:51 AM -
help w words
By Gilgamesh in forum New To JavaReplies: 5Last Post: 11-21-2007, 06:15 PM -
Jumble 1.0.0
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-18-2007, 05:50 PM -
Aspose.Words for Java - 2.1.0.0
By levent in forum Java SoftwareReplies: 0Last Post: 05-24-2007, 10:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks