-1

I have an exercise where are given a list of 850 basic words in English in the file basicWords.txt. I need to compose a text of 10000 words by randomly selecting words from the basic words list and write it to another file. I generated successfully the words, but I have a problem: I get an exception when the words are generated: ArrayIndexOutOfBoundsException at line 35. Also, how can I print the result into another text file? I have a final solution for this:

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

}
bicanul123
  • 427
  • 7
  • 21

2 Answers2

0

Use the debugging feature of your favorite IDE (might be Eclipse), set an exception breakpoint on ArrayIndexOutOfBoundsException, run your program in debug mode.

When it hits the exception, Eclipse will halt your program. Look at your variable values, especially which array you are accessing and what value the index has, and why it got a value outside of the array size.

By the way, your code line if(n.length()>4){ cannot produce an ArrayIndexOutOfBoundsException, as there's no array indexing in that line.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7
0

Why do you not use collections? According to description this task is very easy especially when don't use bunch for, while loops and meaningless variables like n,t,j. etc.

 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;
    }
fxrbfg
  • 1,756
  • 1
  • 11
  • 17