Java. How to convert random line of characters to a string?

java

Solution

Easiest way would be to use a StringBuilder or StringBuffer (syntax is the same).

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 50; i++) {
    sb.append(alphabet.charAt(r.nextInt(N)));
}
String s = sb.toString();

Problem

I'm new to java. I have started about 3 days ago. I want to make a line of random characters and to put them into a string. Thank you. ``` import java.util.Random; public class test{ public static void main (String[]args){ final String alphabet = "abcdefghigklmnopqrstuvwxyz"; final int N = alphabet.length(); Random r = new Random(); for (int i = 0; i < 50; i++) { String s = alphabet.charAt(r.nextInt(N)); // System.out.println(alphabet.charAt(r.nextInt(N))); }}} ```

Original source

Related problems