Java中的无限猴子定理

时间:2017-12-09 12:17:24

标签: java oop math

我有一个练习,在basicWords.txt文件中给出了850个英语基本单词列表。我需要通过从基本单词列表中随机选择单词并将其写入另一个文件来撰写10000个单词的文本。我成功生成了单词,但是我有一个问题:生成单词时出现异常:第35行ArrayIndexOutOfBoundsException。另外,如何将结果打印到另一个文本文件中? 我有一个最终解决方案:

    package randomstring;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;  
import java.io.FileNotFoundException; 
import java.io.FileWriter;
import java.io.IOException;  
import java.io.InputStreamReader; 
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
 *
 * @author robi1
 */


public class RandomString {

public static void main(String[] args){
     List<String> dictionary = readDictionaryFrom("basicWordsInEnglish.txt");
        List<String> monkeyText = generateTextFrom(dictionary);
        writeTextToFile(monkeyText, "final.txt");
    String letters = "abcdefghijklmnopqrstuvwxyz ";
    Object[] wrds = readFile("basicWordsInEnglish.txt");

    int x = wrds.length;
    String[] words = new String[x];
    for(int i =0;i<x;i++){
        words[i] = wrds[i].toString();
    }
    char[] let = letters.toCharArray();
    String n ="";

    Random r = new Random();
    char t;


}

public static Object[] readFile(String name){

    ArrayList<String> al = new ArrayList<String>();
    FileInputStream fstream;
    try {
        fstream = new FileInputStream(name);


    DataInputStream in = new DataInputStream(fstream);

    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;

    while((strLine=br.readLine())!=null){
        if(strLine.length()>4)
            al.add(strLine);

    }
    fstream.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Object[] array  = al.toArray();

    return array;


}
public static List<String> readDictionaryFrom(String path) {
    try {
            return Files.readAllLines(new File(path).toPath());
        } catch(IOException e) {
            throw new RuntimeException(e);
        }
}

public RandomString(List<String> text, String path) {
    try(BufferedWriter file = new BufferedWriter(new FileWriter(new File(path)))){
        for(String word : text) {
            file.write(word+" ");
        }
    } catch(IOException e) {
        throw new RuntimeException(e);
    }
}

public static List<String> generateTextFrom(List<String> words) {
       Random generator = new Random();
        List<String> result = new ArrayList<>();
        for(int i = 0; i < 10000; i++) {
            int random = generator.nextInt(words.size());
            result.add(words.get(random));
        }
        return result;
}



    public static void writeTextToFile(List<String> text, String path) {
        try(BufferedWriter file = new BufferedWriter(new FileWriter(new File(path)))){
            for(String word : text) {
                file.write(word+" ");
            }
        } catch(IOException e) {
            throw new RuntimeException(e);
        }
    }

}

2 个答案:

答案 0 :(得分:0)

使用您喜欢的IDE(可能是Eclipse)的调试功能,在ArrayIndexOutOfBoundsException上设置异常断点,在调试模式下运行程序。

当它遇到异常时,Eclipse将暂停你的程序。查看您的变量值,尤其是您正在访问的数组以及索引具有的值,以及为什么它得到的值超出了数组大小。

顺便说一句,您的代码行if(n.length()>4){无法生成ArrayIndexOutOfBoundsException,因为该行中没有数组索引。

答案 1 :(得分:0)

为什么不使用收藏品?根据描述,这个任务非常简单,特别是当不使用束for, while循环和无意义的变量如n,t,j时。等。

 public void main(String... args) {
        List<String> dictionary = readDictionaryFrom("path to dictionary");
        List<String> monkeyText = generateTextFrom(dictionary);
        writeTextToFile(monkeyText, "path to destination file");
    }

    public List<String> readDictionaryFrom(String path) {
        try {
            return Files.readAllLines(new File(path).toPath());
        } catch(IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void writeTextToFile(List<String> text, String path) {
        try(BufferedWriter file = new BufferedWriter(new FileWriter(new File(path)))){
            for(String word : text) {
                file.write(word);
            }
        } catch(IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static List<String> generateTextFrom(List<String> words) {
        Random generator = new Random();
        List<String> result = new ArrayList<>();
        for(int i = 0; i < 10_000; i++) {
            int random = generator.nextInt(words.size());
            result.add(words.get(random));
        }
        return result;
    }