B Whitehead
B Whitehead

Reputation: 49

Combining random characters to make a string Java

I am trying to make a program that combines a group of characters that are randomly grabbed from a pool to a string that is called a name. The problem that I currently have is that it only makes one letter, so when it ends up printing its only 1 letter, when it should be more.

  //Determining First Name
  for(int g = 0; g < NAME_LENGTH_FIRST; g++) {
    char randomChar = pool[random.nextInt((pool.length) - 1) + 1];
    PERSON_NAME_FIRST = new StringBuilder().append(randomChar).toString();
  }

How would you recommend I go about fixing this?

Upvotes: 0

Views: 140

Answers (1)

Andrei Rusu
Andrei Rusu

Reputation: 46

StringBuilder partialName = new StringBuilder();

for(int g = 0; g < NAME_LENGTH_FIRST; g++) {
    char randomChar = pool[random.nextInt((pool.length) - 1) + 1];
    partialName.append(randomChar);
}

PERSON_NAME_FIRST = partialName.toString();

Upvotes: 2

Related Questions