This is the Java
SCRIPT forum. About the only thing that
JAVA (your code) and JavaScript have in common are the first 4 letters of their names.
But before you post in the right forum, some advice:
NOBODY could figure out FOR SURE from the code you have shown what your problem is.
You never show how
variable2 is created and/or what values it might have.
But even having said that, your code makes no sense. And I *think* I see what is happening.
You say "and array of 4 random numbers is generated". Yes, I see that. In the array named
boomhauer.
You then say "THEN another array of 4 strings is generated". No, that is not true.
You only ever set the value of *ONE* of the
hank[..] strings.
There is NO LOOP in the code that does the
hank[variable2] = "..." stuff. So ONLY ONE of those array entries will be given a string value. The rest WILL be null.
And we know what the value of variable1 will be when that is done: It will *ALWAYS* be 4! Because you ran this code:
Code:
do
{
boomhauer[variable1] = (int)(Math.random() * 6) + 1;
variable1++;
}
while (variable1 <= 3);
and at the end of it,
variable1 will *ALWAYS* have a value of 4.
So when you get to the series of "if" conditions, the ONLY one that will be executed is this one:
Code:
} else if (variable1 == 4) {
hank[variable2] = "Brown";
So one and only one of the elements of the
hank[] array will have the string "Brown".
And since you NEVER INITIALIZE OR CHANGE the value of
variable2, that means that doing
Code:
hank[variable2] = "Brown";
is actually doing
And THAT is why you always get "Brown" as the one and only string that is displayed.
**********
In short, throw all that code away and start over. It's utter trash.
Code:
String[] colors = {"Red","Blue","Green","Brown","Yellow","White"};
for ( int i = 0; i <= 3; ++i )
{
int rnd = (int)(Math.random() * 6);
boomhauer[i] = rnd + 1;
hank[i] = colors[rnd];
System.out.println(hank[i]); // to demo that it worked
}
There. That just did the same thing your code purported to do, in one heluva lot less code.
******
EDIT: I see this thread has been moved to the Java forum. Good.