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);
}
}