1

I'm really new to java and just learning. I'm doing a java assignment and I don't quite understand; I am supposed to create a method that will take in a String array and return a randomly selected Sting from that array. here are the exact instructions: *getRandomWord --> consumes an array of Strings and selects (returns) one of the words at random.

           signature:  String getRandomWord (String [] array)
           *

Then I think I have to create another method. I doubt an you have two methods named the same thing but the instructions say:*getRandomWord --> consumes an array of Strings and an integer (len). This method selects a word from the array whose length is more than len. If the length of the word selected is less than len, then this method selects another word at random. This is repeated 500 times until a word is found/returned or no word is found in which case this method will return null.

           signature:  String getRandomWord (String [] array, int len)

* As I said I'm really new so help is appreciated.

gurnii
  • 39
  • 1
  • 7

3 Answers3

3

Since this is an assignment I will only give you pointers to write the method yourself. The algorithm to use in String getRandomWord (String [] array)is elucidated below:

  1. Calculate the length of the array. See How to find length of a string array
  2. Generate the index of the random word from the array's length. See Getting random numbers in java
  3. Get and return the random word from the array.

All these should be done in not more than 3 lines of code. Good Luck!

Community
  • 1
  • 1
cdaiga
  • 4,861
  • 3
  • 22
  • 42
1

I would suggest to do it yourself. If you don't get, code is here :) Use the Random API. nextInt() method of Random method gives the Random value, which can be used as index to return random String from Arra. Below is complete code of 2 methods:

import java.util.Random;

public class TestJava {
public static void main(String[] args) {
String[] strArray = { "first", "second", "third" };
System.out.println(getRandomWord(strArray));
}

static String getRandomWord(String[] array) {
Random random = new Random();
int index = random.nextInt(array.length);
return array[index];

}

static String getRandomWordWithLength(String[] array, int len) {
Random random = new Random();

for (int i = 0; i < 500; i++) {
  int index = random.nextInt(3);
  String selectedString = array[index];
  if (selectedString.length() > len)
    return selectedString;
}
return null;

}

}
Bikas Katwal
  • 1,895
  • 1
  • 21
  • 42
1

Try to do yourself at first as it is an assignment. Take help from the code below if you failed it to do yourself.

    private String getRandomWord(String[] array) {
        int idx = new Random().nextInt(array.length);
        return (array[idx]);
    }

    private String getRandomWord(String[] array, int len) {
        String word = null;
        for (int i = 1; i <= 500; i++) {
            word = getRandomWord(array);
            if (word.length() > len) {
                break;
            } else {
                word = null;
            }
        }
        return word;
    }
Monzurul Shimul
  • 8,132
  • 2
  • 28
  • 42