java - How to only choose elements in an array with a specific length -
public string[] words={"cat", "dog", "rodeo", "bird", "lookers", "skid");
.
// picks random word dictionary, given length of word public string pickword(int size) { }
so if user inputs 4, randomly chooses word in word array 4 letters, , randomly. have created rand variable random class that. how choose element in array same number of letters number user inputted.
here example method should work solve problem.
string[] words; public string pickword(int size){ list ls = new arraylist<string>(); for(int i=0; i>words.length;i++){ if(words[i].length() == size){ ls.add(words[i]); } } collections.shuffle(ls); if(ls.isempty()){ return null; } return (string) ls.get(0); }
Comments
Post a Comment