从文本文件中随机选择10个单词

时间:2016-12-19 12:19:11

标签: java arrays string bufferedreader fileinputstream

我正在尝试创建一个读取两个文本文件的程序,这两个文件都包含数千个单词。我需要能够从每个文件中随机选择10个单词并将它们存储在四个字符串列表的数组中。到目前为止,我已经创建了以下代码,但是这只从每个文件中选择一个单词,而不是10.如何才能完成 - 最好使用if语句?

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 个答案:

答案 0 :(得分:1)

使用Math.random()生成10个随机数,并按数字数组的大小计算它们(因为它们是介于0和1之间的值),并使用这些数字从数组中选择项目。例如,尝试循环这10次:Array[Math.random()*sizeOfArray]

答案 1 :(得分:0)

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