Tuesday, 15 June 2010

java - Trouble getting return type from string builder in final method for word scramble game -



java - Trouble getting return type from string builder in final method for word scramble game -

i have create word scrambler "game" , have methods need use. still need set code in main method asking users guess , using .equals see if right word. that's not problem. i'm running issue on scrambleword(word) method. used string builder , cannot figure out how return. code looks , builds says there error. can help? code of now.

import java.io.bufferedreader; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception; import java.util.arraylist; import java.util.arrays; import java.util.list; import java.util.random; /** * * @author brian2 */ public class project3 { /** * @param args command line arguments * @throws java.io.ioexception */ public static void main(string[] args) throws ioexception { //shuffle s = new shuffle(); // s.shuffle("hello"); string[] words = createlistofwords(); string word = chooserandomword(words); string scrambledword = scrambleword(word); system.out.println(scrambledword); /* code asking userers guess * *use .equals * * */ } double[] wordlist = new double[65573]; private static string[] createlistofwords() throws filenotfoundexception, ioexception { bufferedreader in = new bufferedreader(new filereader("wordlist.txt")); string str; list<string> list = new arraylist<string>(); while ((str = in.readline()) != null) { list.add(str); } string[] stringarr = list.toarray(new string[0]); // test see if array stored--system.out.println(arrays.tostring(stringarr)); homecoming stringarr; } /** * * @param words * @return */ private static string chooserandomword(string[] words) { int index = new random().nextint(words.length); string word = words[index]; homecoming word; } private static string scrambleword(string word) { char[] chararray = word.tochararray(); list<character> characters = new arraylist<>(); stringbuilder scrambledword = new stringbuilder(word.length()); int randpicker = (int) (math.random() * characters.size()); stringbuilder append = scrambledword.append(characters.remove(randpicker)); /*for (int = chararray.length - 1; > 0; i--) { int j = ((int)(math.random())(i + 1)); double scrambledword = chararray[i]; chararray[i] = chararray[j]; chararray[j] = (char) scrambledword; }*/ homecoming scrambledword.tostring() ; /*this method should take word argument, , homecoming same word, except scrambled. in order scramble word, convert array of chars, this:*/ //char[] chararray = word.tochararray(); } }

you never added characters list in scrambleword. utilize collections.shuffle(list) , like

private static string scrambleword(string word) { list<character> characters = new arraylist<>(); (char ch : word.tochararray()) { characters.add(ch); } collections.shuffle(characters); stringbuilder sb = new stringbuilder(); (char ch : characters) { sb.append(ch); } homecoming sb.tostring(); }

java methods stringbuilder return-type

No comments:

Post a Comment