1

I currently have a project for my computer science class that requires me to choose a random word from a text file. So far I've gotten the text file into an array and currently can print the entire thing, yet for the life of me I cannot figure out to grab a random word from it..? I know the code is messy and unorganized, sorry. Thanks in advance! <3

package moviemain1;

/**
*
* @author rogerseva
 */
import java.io.*;
import java.util.*;

public class MovieMain1 {

/**
 * @param args the command line arguments
 * @throws java.io.IOException
 */
public static void main(String[] args) throws IOException {
    Scanner scan = new Scanner(new File("movies.txt"));
    int numOfMovies = 0;
    int randomMovie = 0;
    String movies = "";
    String s = scan.nextLine();
    args = s.split("");
    ArrayList<String> movieList = new ArrayList<String>();



     while (scan.hasNextLine()) {
     movieList.add(scan.nextLine());
    }

    while (scan.hasNextLine()) {
        String line = scan.nextLine();
        movies += (line + "\n");
        numOfMovies++;
        randomMovie = (int) (Math.random() * numOfMovies);
    }


    System.out.println(movieList);




     }
   }

3 Answers3

3

ThreadLocalRandom.current().nextInt

Use this to get a random number. Then just use ArrayList's get method to get the random item.

Here's a little demonstration

import java.util.ArrayList;
import java.util.concurrent.ThreadLocalRandom;
public class Movies {
    public static void main(String[] args){
        ArrayList<String> movieList = new ArrayList<String>();
        movieList.add("Movie1");
        movieList.add("Movie2");
        movieList.add("Movie3");
        for (int i = 0; i < 100; i++) {
            int x = ThreadLocalRandom.current().nextInt(0, movieList.size());
            System.out.println(movieList.get(x));
        }
     }
}

From Java 7 onward, prefer java.util.concurrent.ThreadLocalRandom to java.util.Random in all circumstances - it is backwards compatible with existing code, but uses cheaper operations internally. info here

meyi
  • 7,574
  • 1
  • 15
  • 28
1

Let's just start with...

ArrayList<String> movieList = new ArrayList<String>();

while (scan.hasNextLine()) {
    movieList.add(scan.nextLine());
}

while (scan.hasNextLine()) {
    String line = scan.nextLine();
    movies += (line + "\n");
    numOfMovies++;
    randomMovie = (int) (Math.random() * numOfMovies);
}

This doesn't make sense, because the second while-loop will never execute, because the first while-loop's exit condition is the same as the second.

Also, movieList.size() will return the number of elements, so I don't understand the point of the second while-loop, especially considering you could just combine them...

while (scan.hasNextLine()) {
    String line = scan.nextLine();
    movieList.add(line);
    movies += (line + "\n");
    numOfMovies++;
    randomMovie = (int) (Math.random() * numOfMovies);
}

Choosing random word from an ArrayList?

After you've filled the ArrayList, you could simply do...

Collections.shuffle(movieList);

The List is now randomised. You could just then use either movieList.get(0), or if want to continue picking random elements from the list without getting a repeated value, movieList.remove(0).

This will, of course, randomise the original list. If you want to keep the original order, then I'd use a second ArrayList...

ArrayList<String> randomised = new ArrayList<String>(movieList);
Collections.shuffle(randomised);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Generate a random int (probably using java.util.Random) from 0 to the length of your ArrayList (exclusive). Then you can simply print out whatever String is at that index in the ArrayList.

Random r = new Random();
int randIndex = r.nextInt(movieList.size());
Nick Silvestri
  • 191
  • 1
  • 15