Q:

Use Java for loop and create variable from output

I'm trying to make a variable out of the result of a for loop. First of all is this possible? If it is possible how do i do it? This is my code so far:
Random r = new Random();
Random n = new Random();

int p = (int)(n.nextInt(14));

for (int i = 0; i < p; i++) {
  char c = (char) (r.nextInt(26) + 'a');
  System.out.println(c);}

  String word = ("output of forloop goes here");

I want to put all the randomly generated letters into one word using the for loop. How do I do this?

A:

Use StringBuilder :
StringBuilder sb = new StringBuilder();
for (int i = 0; i < p; i++) {
  char c = (char) (r.nextInt(26) + 'a');
  sb.append(c);
}

Then you can call sb.toString() to get the resulting String.

