String[] n = new String[8];
String name = n[0] + n[1] + n[2] + n[3] + n[4] + n[5] + n[6] + n[7];
for(int x = 0; x < 8; x++)
{
int h = 97;
char j = (char) h;
n[x] = String.valueOf(j);
}
System.out.println(name);
So I was experimenting with randomizing names but kept getting nulls returned so I tried narrowing down the problem as well as I could and used h = 97
as a constant (this is the variable I was initially going to randomise) so I could be certain it couldn't go out of range. However something seems to go wrong in the for/loop because I tried creating the name
string manually by adding values like n[0] = String.valueOf(v);
on several lines and it worked perfectly.
Any help?
You're concatenating the values in the array and assigning the result to name
before you modify any of the values within the array... i.e. when every element of the array is null
. Just move this line:
String name = n[0] + n[1] + n[2] + n[3] + n[4] + n[5] + n[6] + n[7];
to after the loop instead of before it.
To make life simpler, you might want to use a char
array instead:
char[] array = new char[8];
for (int x = 0; x < 8; x++)
{
array[x] = (char) 97; // Or whatever
}
String name = new String(array);
System.out.println(name);
See more on this question at Stackoverflow