jeudi 18 juillet 2019

How to create random length String containing multiple single letter?

The task of mine is to create a random lenght A letter generator. It has to create 50 lines of Strings containing random number of A letter. To be honest, I think I am really close to conclusion, but still I know I am missing something. Currently the generator of mine is creating random numbers.

The task is concentrated about doing it with for loop.

Futhermore - if there is easier way to write the code of mine than that what I have done - i will be greatful for tips.

I tried diffrent way of expressing the for loop, none of them got me close to my task.

import java.util.*;
import java.lang.*;
import java.io.*;

class RandomGenerator{
    private static final String alpha = "a";
    private static final String data = alpha;

    private static final String combinationOFA = alpha;

    public static void main(String[] args) {


    for (int n=0; n<50; n++){
        Random theGeneratorForLength = new Random();
    }

    System.out.println("String : " + combinationOFA);
}

public static String generateRandomALetter(int length) {
Random theGeneratorAlpha = new Random();

StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {

    int aLengthVariable = 
    theGeneratorAlpha.nextInt(combinationOFA.length());
    char alphaTeta = combinationOFA.charAt(aLengthVariable);

    sb.append(alphaTeta);
    }

return sb.toString();

}

}

I would like this code to create 50 Strings made of random lenght multiplied A letter




Aucun commentaire:

Enregistrer un commentaire