-1

I'm doing a simple random quiz app in Android. So basically I have string array of words. I need to display the string without repetition. This is what I've tried so far:

String[] words = { "Welcome", "Different", "Teenager", "Transfer", "Italian", 
                   "Timber", "Toxic", "Illiterate", "Irate", "Moderate", "Transportation", "Attention" };


ArrayList<String> wordlist = new ArrayList<String>();
for (String i : words) 
    wordlist.add(i);
Collections.shuffle(wordlist);

randomStr = words[new Random().nextInt(words.length)];

tvWord.setText("");
tvWord.setText(randomStr);

But I still get the random word repeating. What am I doing wrong in here? Any ideas? I would gladly appreciate your help. Thanks.

Update:

First on a button click the word should then display. And many times I click the button I keep getting the same word again.

 switch(v.getId()){
    case R.id.btPlay:
        randomWordList();
        break;
 }

Where randomWordList(); is the method I posted above.

Dunkey
  • 1,900
  • 11
  • 42
  • 72

4 Answers4

2

You don't keep track of what was already used. I also don't understand why you define wordlist and then don't use it. Just a small modification of your code will do the trick.

Edit: You also need to initialize values in different scope than where you use it. For example like this:

public class MyClass{

LinkedList<String> wordlist;

public MyClass(){
    String[] words = { "Welcome", "Different", "Teenager", "Transfer", "Italian", 
               "Timber", "Toxic", "Illiterate", "Irate", "Moderate", "Transportation", "Attention" };
    wordlist = new LinkedList<String>();
    for (String i : words) 
        wordlist.add(i);
    Collections.shuffle(wordlist);
}

public void useNextWord(){
    tvWord.setText("");
    tvWord.setText(wordlist.pollLast());
}
NeplatnyUdaj
  • 6,052
  • 6
  • 43
  • 76
  • I tried this but when I click the button many times the word keeps repeating again – Dunkey Feb 28 '14 at 17:29
  • You need to post more than that. If this is in one method, the it won't work. The words need to be initialized elsewhere(some init method or constructor). Otherwise you are losing the information of what was already used. – NeplatnyUdaj Feb 28 '14 at 17:30
  • Good answer. You should check if the list is empty before trying to retrieve and display the next string, though. – Claus Feb 28 '14 at 17:54
  • 1
    It will return null then. No biggie:) i don't know what op is going to do with it – NeplatnyUdaj Feb 28 '14 at 17:59
1

Create a new list of Strings and shuffle it. Then simply iterate over the shuffled list from the beginning.

ArrayList<String> copyOfWords= new ArrayList<String>(words);
Collections.shuffle(copyOfWords);

This is taken from here. Time complexity is O(n).

To iterate over the List you can either get the first element and remove it (with the same method) or keep track which was the last used element.

First solution:

String randomWord = copyOfWords.remove(0);
tvWord.setText(randomWord);

P.S. do not use this line in your code randomStr = words[new Random().nextInt(words.length)];

Community
  • 1
  • 1
Tautvydas
  • 2,027
  • 3
  • 25
  • 38
0

You will need to either keep track of the Strings you have already used in another array or list and check the value has not already been used or alternatively remove any used Strings from the pool. You can do that with String next = List.remove(index).

indivisible
  • 4,892
  • 4
  • 31
  • 50
0

You first shuffle your list with Collections.shuffle(wordlist); and then access the list with a random index. There's no need to use a random index if the list is already shuffled. Just get one item after another. You could use wordlist.remove(0) until your list is empty.

Display the next string as follows:

if (!wordlist.isEmpty()) {
    randomStr = wordlist.remove(0);
    tvWord.setText(randomStr);
}
Claus
  • 150
  • 8
  • Can you give sample implementation of this? – Dunkey Feb 28 '14 at 17:31
  • @Dunkey: Updated my answer. This is how you retrieve and display the next string from the shuffled list. The string is removed from the list so it's not displayed twice. – Claus Feb 28 '14 at 17:46