Jump to content


Arrayindexoutofbounds Exception


  • Please log in to reply
1 reply to this topic

#1 Capital Wasteman

Capital Wasteman

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 12 June 2012 - 05:34 PM

I'm trying to make a Random Story Generator. This program should read in a text file, choose a random word to begin with, then put other words in place based on what words follow them throughout the file. My program builds fine, however, when I run it, it gives me this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at Random_Story_Generator.Reader(Random_Story_Generator.java:34)
    at Random_Story_Generator.main(Random_Story_Generator.java:15)
Keep in mind, this program is far from complete, I just need help with this one thing. My text file is exactly 1403 words long, so that is the reason for the specific numbers.
 import java.io.*;
 import java.util.Random;
 import java.lang.String;
public class Random_Story_Generator {
	static String temp[] = new String[1403];
	static int numbers[] = new int[1403];
	static String words[][] = new String[1403][1403];
	public static void main(String[] args) throws IOException {
		Reader();
		Creator();
	}
	public static void Random(){
		int x = 0;
		for(int y = 0; y < x; y++){
			Random rndNumGen = new Random();
			numbers[y] = rndNumGen.nextInt(1403);
		}
	}
	public static void Reader() throws IOException, FileNotFoundException{
		String line;
		BufferedReader input;
		input = new BufferedReader(new FileReader("H:\\RSGLyrics.txt"));
		for(int c = 0; c < temp.length; c++){
			line = input.readLine();
			temp = line.split(" ");
		}
		for(int h = 0; h <= temp.length; h++){
			System.out.print(" " + temp[h]);		}
	}
	public static void Creator(){
		int y = 0;
		Random rndNumGen = new Random();
		y = rndNumGen.nextInt(1403);
		System.out.print(words[y]);
		}
}


#2 Kraicheck

Kraicheck

    Advanced Member

  • Members
  • PipPipPip
  • 879 posts
  • Gender:Male
  • Location:Belgium

Posted 13 June 2012 - 06:58 AM

What do you think the exception means?
If you understand its meaning, go look at line 34 in your code and around it to find out why this exception occurs.