Randomly select 10 words from a text file

911 views Asked by At

I am attempting to create a program that reads two text files, which both contain thousands of words. I need to be able to select 10 words at random from each file and store them in an array of four string lists. I have created the following code so far, however this only selects one word from each file, not 10. How can this be done - preferably using an if statement?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class RandomWordGenerator {

public static void main(String[] args) throws FileNotFoundException {

public class RandomWordGenerator {
public static void main(String[] args) throws IOException {
    Path outputFile = Paths.get("output.txt");
    ArrayList<String> randomWords1 = randomWordsFromFile("textfile1.txt", 10);
    ArrayList<String> randomWords2 = randomWordsFromFile("textfile2.txt", 10);
    OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(outputFile, CREATE));
    System.out.println(randomWords1);
    System.out.println(randomWords2);
    outputStream.flush();
    for (int i = 0; i < randomWords1.size(); i++) {
        outputStream.write(randomWords1.get(i).getBytes());
    }
    for (int i = 0; i < randomWords2.size(); i++) {
        outputStream.write(randomWords2.get(i).getBytes());
    }
    outputStream.close();
}

private static ArrayList<String> randomFromFile(String fileName, int count) throws FileNotFoundException {
    Scanner scanner = new Scanner(new File(fileName));
    ArrayList<String> words = new ArrayList<>();
    while (scanner.hasNext()) {
        words.add(scanner.next());
    }
    return randomFromWords(words, count);
}

static private ArrayList<String> randomFromWords(ArrayList<String> words, int count) {
    ArrayList<String> randomWords = new ArrayList<>();
    for (int i = 0; i < count; ) {
        int random = new Random().nextInt(words.size());
        if (randomWords.add(words.get(random))) {
            i++;
        }
    }
    return randomWords;
   }
}
2

There are 2 answers

10
Gopinath Langote On BEST ANSWER
import java.io.*; 
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

import static java.nio.file.StandardOpenOption.CREATE;

public class RandomWordGenerator {
    public static void main(String[] args) throws IOException {
        Path outputFile = Paths.get("output.txt");
        ArrayList<String> randomWords1 = randomWordsFromFile("input1.txt", 10);
        ArrayList<String> randomWords2 = randomWordsFromFile("input2.txt", 10);
        OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(outputFile, CREATE));
        System.out.println(randomWords1);
        System.out.println(randomWords2);
        outputStream.flush();
        for (int i = 0; i < randomWords1.size(); i++) {
            outputStream.write(randomWords1.get(i).getBytes());
        }
        for (int i = 0; i < randomWords2.size(); i++) {
            outputStream.write(randomWords2.get(i).getBytes());
        }
        outputStream.close();
    }

    private static ArrayList<String> randomWordsFromFile(String fileName, int count) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File(fileName));
        ArrayList<String> words = new ArrayList<>();
        while (scanner.hasNext()) {
            words.add(scanner.next());
        }
        return randomFromWords(words, count);
    }

    static private ArrayList<String> randomFromWords(ArrayList<String>     words, int count) {
        ArrayList<String> randomWords = new ArrayList<>();
        for (int i = 0; i < count; ) {
            int random = new Random().nextInt(words.size());
            if (randomWords.add(words.get(random))) {
                i++;
            }
        }
        return randomWords;
    }
  }
3
GarryNewman On

Generate 10 random numbers using Math.random() and times them by the size of your array of the numbers (as they are a value between 0 and 1) and use these numbers to select items from your array. For example try looping this 10 times: Array[Math.random()*sizeOfArray]