Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class Main {
    public static String randomMessages[] = { "Message 1", "Message 2", "LOL", "asdf" };

    public static void main(String[] args) {
        int howManyWords = 2;
        List<String> listOfWords = new ArrayList<>();
        Random random = new Random();
        listOfWords.addAll(Arrays.asList(randomMessages));
        List<String> selectedRandomMessages = new ArrayList<>();
        for (int i = 0; i < howManyWords; i++) {
            int randomNumber = random.nextInt(listOfWords.size());
            String randomItem = listOfWords.get(randomNumber);
            selectedRandomMessages.add(randomItem);
            listOfWords.remove(randomItem);
        }
        System.out.println(selectedRandomMessages);
    }
}