1
String [] words = { "apple", "orange", };
String Word = words[wordGenerator];            //wordsGenerator is just a rand(); which return a integer.

String [] letters;
List<String> wordList;

letters = new String[12];
wordList = new ArrayList<String>(Arrays.asList(letters));

letters = Word.split("(?!^)");

By doing the above code I know that I'll get a Array called 'letters' with the first 5 memory as a, p, p, l, e if it generate apple am I right?

If so, how can I pass this 5 strings from the 'letters' array into the wordList? The solution seems somehow easy but I just couldn't come out with it. Any help would be greatly appreciated. Thank you.

2 Answers 2

1
 for(int i=0;i<letters.length();i++{
 wordlist.add(letters[i]);
 }

I think.. this is what you were asking...?

1
  • thanks raju. clode enough. ;) i change the add to set instead. for(int i = 0; i < letters.length; i++) {wordList.set(i,letters[i]);} Commented Apr 24, 2012 at 9:42
0

Here a snippet code:

/**
 * @param args
 */
public static void main(String[] args) {
    //init the wordList
    List<String> wordList=new ArrayList<String>();
    //init the word buffer
    String [] letters = new String[12];
    //init the data
    String [] words = { "apple", "orange", };
    //pick a random number
    int wordsGenerator = new Random().nextInt(words.length);
    //pick a random word
    String Word = words[wordsGenerator];            
    //process the word...
    letters = Word.split("(?!^)");
    //add the word to the wordList
    String mergedWord = join(letters);
    wordList.add(mergedWord);

}

/*
 * Merges a set of strings to one string
 */
static String join(String[] strs){
    StringBuffer sb=new StringBuffer();
    for (String item : strs) {
        sb.append(item);
    }
    return sb.toString();
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.