I must be overlooking something simple here, but I have no clue why it says that it is going out of bounds.

Here is my entire program...

import java.util.Scanner;
import java.util.Random;

public class MadLib
{
	static Scanner input = new Scanner(System.in);
	static Random rng = new Random();
	static String t[] = {"Nouns", "Verbs", "Direct Objects", "Prepositional Phrases"};
	static int max;
	
    public static void main(String args[])
    {
    	System.out.print("How many random sentences would you like? ===> ");
        max = input.nextInt();
    	
    	String dump = input.nextLine();
    	System.out.println();
    	
    	String nouns[] = new String[max];
    	String verbs[] = new String[max];
    	String dO[] = new String[max];
    	String pP[] = new String[max];
    	String rS[] = new String[max*4];
    	
    	
    	create(nouns, verbs, dO, pP, t);
  	    createRandomSentence(nouns, verbs, dO, pP, rS);
    	display(nouns, verbs, dO, pP, rS);   
    	
    	}
    	
    	public static void create(String t[], String nouns[], String verbs[], String dO[], String pP[])
    	{
    		int i=0;
    		
    		while(i<=0)
    		{
    			int p=1;
    			while(p<max)
    			{
    				System.out.print("Please input a noun: ");
    				String firstq = input.nextLine();
    				System.out.println();
    				nouns[p] = firstq;
    				p++;
    			}
    			i++;
    		}
    		
    		
    		
    		while(i<=1)
    		{
    			int p=1;
    			while(p<max)
    			{
    				System.out.print("Please input a verb: ");
    				String secondq = input.nextLine();
    				System.out.println();
    				verbs[p] = secondq;
    				p++;
    			}
    			i++;
    		}
    		
    		
    		
    		while(i<=2)
    		{
    			int p=1;
    			while(p<max)
    			{
    				System.out.print("Please input a direct object: ");
    				String thirdq = input.nextLine();
    				System.out.println();
    				dO[p] = thirdq;
    				p++;
    			}
    			i++;
    		}
    		
    		
    		
    		while(i<=3)
    		{
    			int p=1;
    			while(p<max)
    			{
    				System.out.print("Please input a prepostitional phrase: ");
    				String fourthq = input.nextLine();
    				System.out.println();
    				pP[p] = fourthq;
    				p++;
    			}
    		i++;
    		}
    		
    	}
    
		public static void createRandomSentence(String nouns[], String verbs[], String dO[], String pP[], String rS[])
		{
			int i=0;
			while(i<max)
			{
				int q=1;
				while(q<=(4*max))
				{
					rS[q] = nouns[rng.nextInt(max)];
					rS[q+1] = verbs[rng.nextInt(max)];
					rS[q+2] = dO[rng.nextInt(max)];
					rS[q+3] = pP[rng.nextInt(max)];
					q+=4;
				}
				
				i++;
			}
		}
		
		public static void display(String nouns[], String verbs[], String dO[], String pP[], String rS[])
		{
			System.out.println(nouns[rng.nextInt(max)]);
			for(int i=1; i<=(max*4); i+=4)
			{
				System.out.println(rS[i] + " " + rS[i+1] + " " + rS[i+2] + " " + rS[i+3]);
			}
			
		}

}

and the part of it that is out of bounds is the rS[q+3] at...

public static void createRandomSentence(String nouns[], String verbs[], String dO[], String pP[], String rS[])
		{
			int i=0;
			while(i<max)
			{
				int q=1;
				while(q<=(4*max))
				{
					rS[q] = nouns[rng.nextInt(max)];
					rS[q+1] = verbs[rng.nextInt(max)];
					rS[q+2] = dO[rng.nextInt(max)];
					rS[q+3] = pP[rng.nextInt(max)];
					q+=4;
				}
				
				i++;
			}
		}

Because q+3 is longer than the total length of your rS array. You cannot access an array at an index longer than it's largest index. This is probably because your 'max' variable isn't a good indicator of rS's length (you are using 'max' as a bound on how large q+3 can be, which makes the assumption that max is always, at most, 3 less than the size of rS - which I'm guessing it is not since you went out of bounds).

If that didn't make sense, let me rephrase: You need to make sure that your 'max' variable isn't going to allow 'q' to become large enough that q+3 is larger than the length of your array. If q+3 is, at any point, larger than the length of your array, you are going to go out of bounds. (PS keep in mind that arrays are indexed by length-1 so you should actually make sure that q+4 isn't larger than the length of your array).

I have

String rS[] = new String[max*4];

which sets the array length to 4 max's so the array length is right. I also have the q+=4 so that it goes by groups of four.

It's because you're trying to access an array location that hasn't been established yet.

example:
if you enter the number 2 (fed into max by nextInt here) the array is set to a size of max*4 = 8. So following the while loop through, you'll see that at its last iteration (when q is 5):
rS[q+3] which = rS[8]
which can't be manipulated because the array was only allocated to be 8 integer values big.

you only have access to rS[0] through rS[7] (8 values).

if you want to allocate space for 1 more integer, change
String rS[] = new String[max*4]; to
String rS[] = new String[(max*4) + 1]; which will initialize the size to carry the extra space.

Alright i changed around a couple things. I know arrays start from 0. I just skipped ahead one so I could subtract from the RNG and not have an OBOB. I've got it solved now though.

What is an OBOB? And if it is solved, mark as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.