Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的无限猴子定理_Java_Oop_Math - Fatal编程技术网

Java中的无限猴子定理

Java中的无限猴子定理,java,oop,math,Java,Oop,Math,我有一个练习,其中在basicWords.txt文件中列出了850个英语基本单词。我需要通过从基本单词列表中随机选择单词,并将其写入另一个文件,从而合成一个10000单词的文本。我成功地生成了单词,但我有一个问题:当生成单词时,我得到一个异常:ArrayIndexOutOfBoundsException,在第35行。另外,如何将结果打印到另一个文本文件中? 我有一个最终解决方案: package randomstring; import java.io.BufferedReader; i

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

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

}
包随机字符串;
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.DataInputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.nio.file.Files;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Random;
/**
*
*@作者robi1
*/
公共类随机字符串{
公共静态void main(字符串[]args){
List dictionary=readDictionaryFrom(“basicWordsInEnglish.txt”);
List monkeyText=generateTextFrom(字典);
writeTextToFile(monkeyText,“final.txt”);
字符串字母=“abcdefghijklmnopqrstuvxyz”;
Object[]wrds=readFile(“basicWordsInEnglish.txt”);
int x=wrds.length;
字符串[]字=新字符串[x];
对于(int i=0;i4)
新增(斯特林);
}
fstream.close();
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
Object[]数组=al.toArray();
返回数组;
}
公共静态列表readDictionaryFrom(字符串路径){
试一试{
返回Files.readAllLines(新文件(path).toPath());
}捕获(IOE异常){
抛出新的运行时异常(e);
}
}
公共随机字符串(列表文本、字符串路径){
try(BufferedWriter file=new BufferedWriter(new FileWriter(new file(path))){
for(字符串字:文本){
file.write(word+“”);
}
}捕获(IOE异常){
抛出新的运行时异常(e);
}
}
公共静态列表generateTextFrom(列表字){
随机生成器=新随机();
列表结果=新建ArrayList();
对于(int i=0;i<10000;i++){
int random=generator.nextInt(words.size());
result.add(words.get(random));
}
返回结果;
}
公共静态void writeTextToFile(列表文本、字符串路径){
try(BufferedWriter file=new BufferedWriter(new FileWriter(new file(path))){
for(字符串字:文本){
file.write(word+“”);
}
}捕获(IOE异常){
抛出新的运行时异常(e);
}
}
}

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

当遇到异常时,Eclipse将停止您的程序。查看您的变量值,特别是您正在访问的数组和索引的值,以及它为什么得到超出数组大小的值


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

为什么不使用集合?根据描述,这项任务非常简单,特别是在不使用bunch
的情况下,而
循环和无意义的变量,如
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;
    }
public void main(字符串…参数){
列表字典=readDictionaryFrom(“字典路径”);
List monkeyText=generateTextFrom(字典);
writeTextToFile(monkeyText,“目标文件的路径”);
}
公共列表readDictionaryFrom(字符串路径){
试一试{
返回Files.readAllLines(新文件(path).toPath());
}捕获(IOE异常){
抛出新的运行时异常(e);
}
}
公共静态void writeTextToFile(列表文本、字符串路径){
try(BufferedWriter file=new BufferedWriter(new FileWriter(new file(path))){
for(字符串字:文本){
file.write(word);
}
}捕获(IOE异常){
抛出新的运行时异常(e);
}
}
公共静态列表generateTextFrom(列表字){
随机生成器=新随机();
列表结果=新建ArrayList();
对于(int i=0;i<10_000;i++){
int random=generator.nextInt(words.size());
result.add(words.get(random));
}
返回结果;
}

哪一行是第35行?代码的第35行,抱歉:如果(n.length()>4){不幸的是,这是我得到的:,,线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:-11 at randomstring.randomstring.main(randomstring.java:39)"现在是第39行了?请帮助我们帮助你。这不是OP的答案。@nicomp它帮助他学会如何自己解决这个和类似的问题,因为他将无法为他职业生涯中的每个编程错误询问stackoverflow。@RalfKleberhoff这不是答案。这是一个注释。这是注释的目的。公共静态void main方法做的esn不起作用..它说writetexttofile不是一个创建的方法,我不能使用它..@RobinsonChera使该方法静态仍然不起作用@fxrbfgNo主类被找到..最后修复了它,谢谢!