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