0

I have a class in which i declare many words as class variables. And there is a method to choose a random word from those class words. How can i implement getRandomWord properly?

public class Vocabulary
{
   int numOfWords = 2;

   final static word1 = "hello";
   final static word2 = "stack";
   ...

    public String getRandomWord()
   {
        int random = (int)(Math.random() * numOfWords + 1);
        return word + random;
   }
}

I tried to add those words to an ArrayList first and then return the index but i cant understand how to add those words that are already declared in the class to the ArrayList.

  • 1
    Create a `static List words` and there store your words. You can add them in static section manually – Michał Ziober Mar 08 '19 at 00:38
  • How can i store the words in a loop? Or i have to store them manually? – Albert Leibnitz Mar 08 '19 at 00:39
  • See static section questions https://stackoverflow.com/questions/2420389/static-initialization-blocks – Michał Ziober Mar 08 '19 at 00:42
  • Possible duplicate of [Select random Java variable? Is this possible](https://stackoverflow.com/questions/9470104/select-random-java-variable-is-this-possible) – Tom Mar 08 '19 at 00:45
  • To extend what @MichałZiober said: that's how you can get a random element from a list: [Randomly select an item from a list](//stackoverflow.com/q/12487592) – Tom Mar 08 '19 at 00:46
  • `static List words = Arrays.asList("hello", "stack", "overflow");` – Erwin Bolwidt Mar 08 '19 at 00:55

2 Answers2

0

If you just want to have one list of words, you should definitely use a Singleton as a static member of your class. This member should have a List and be created only once. That's why the constructor should be private. It should look like this

import java.util.Arrays;

import java.util.List;
public final class Vocabulary{
  //The final in the class name is there so you can't heritate from this class
  private static Vocabulary INSTANCE = null;
  private List<String> words;

 //Private so you can't create another instance of it, you need to use static method
  private Vocabulary() {
      this.words = Arrays.asList("hello", "stack", "heap");
  }
  //In case you want to implemente other public function
  public static Vocabulary getInstance(){
      if(INSTANCE == null){
          INSTANCE = new Vocabulary();
     }
      return INSTANCE;
  }
 //If you want to choose a word by its number
  public static String getWord(int i){
      return Vocabulary.getVocabulary().get(i);
  }
  //Used in getVocabulary, getter of the List
  private List<String> getWords(){
      return this.words;
  }
  // Return the whole List of words
  public static List<String> getVocabulary(){
      return Vocabulary.getInstance().getWords();
  }
  // Here your go
  public static String getRandomWord(){
      return Vocabulary.getWord((int)(Math.random() * Vocabulary.getVocabulary().size()));
  }
}

And then you can just use it properly :

public static void main(String[] args) {
    System.out.println(Vocabulary.getRandomWord());
}

Singleton is a well known design pattern and this is a pretty clean way to do it, hope it helps !

NohTow
  • 21
  • 3
0

Make array like this:

String[] strs = { "hello","stack"};

Then add then to List<String>.

  List<String> list = Arrays.asList( strs );
Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55