D
D
Dmitry Bobreshov2017-10-02 16:30:36
Java
Dmitry Bobreshov, 2017-10-02 16:30:36

How to implement random text output in Java?

There is a code that displays random text from an array:

String[] texts = {"Привет", "Пока", "Уже виделись"};
Random random = new Random ();
int pos = random.nextInt(texts.length);
System.out.print(texts[pos]);

The question is that I want to randomly display whole sentences of which there will be quite a lot (about 1000), and as I understand it, it’s not possible to write them all in an array separated by commas. Please tell me the best way to implement this?
Maybe create a file where each line will have one sentence and randomly output a line from the file?
Advise how to implement? If there are examples, I will be very grateful.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cyril, 2017-10-02
@is_there_something_wrong

There are many solutions to the problem

F
Fedor Sazonov, 2017-10-02
@Revenant20

Well anyway

String[] texts = {"Привет", "Пока", "Уже виделись"};
  Random random = new Random();
        for (int i = 0; i < 10000; i++) {
            int pos = random.nextInt(texts.length);
            if (i % 10 == 0) {
                System.out.print(texts[pos] + "\n");
            } else {
                System.out.print(texts[pos] + " ");
            }
        }

Add some conditions for adding a period and a comma.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question