How to randomly pick a string from multiple finals strings in Java?

Angelo

As the question says, I've got multiple finals strings in a final class like this:

public final class MyStrings {

    public static final String stringOne = "this is string one";
    public static final String stringTwo = "this is string two";
    public static final String stringThree = "this is string three";
    public static final String stringFour = "this is string four";
    public static final String stringFive = "this is string five";

}

I know it could be easier to put them in a list or an array, but I would like to avoid runtime filling of those structures. Is it possible? What is the simplest way to choose randomly a string from that class? Thank you.

Joffrey

Your Strings seem very related, since you have a use case that needs to randomly choose one among them. Therefore, they should probably be gathered in an array anyway (on a semantic point of view).

There won't be more "runtime filling" than with your current multiple fields:

public final class MyStrings {

    public static final String[] strings = {
        "this is string one",
        "this is string two",
        "this is string three",
        "this is string four",
        "this is string five"
    };
}

Then you can pick one String randomly this way:

Random random = new Random();
String randomString = strings[random.nextInt(strings.length)]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to randomly select a string between two strings from ENUM in java?

From Java

How do I pick randomly from an array?

From Dev

How to pick one key from a dictionary randomly

From Dev

How to randomly get a string from a collection but prefer strings at the beginning of the collection

From Dev

How to display randomly selected strings from an array in java without repetitions

From Dev

How to randomly pick value from a list in Unity3D?

From Dev

How to randomly pick a number of combinations from all the combinations efficiently

From Dev

How to pick less than N objects randomly from K objects?

From Dev

randomly pick from 4 tables

From Dev

How to randomly assign strings from a macro to observations

From Dev

How to randomly assign strings from a macro to observations

From Dev

How to pick multiple images from device?

From Dev

How to pick multiple images from gallery?

From Dev

How to pick capital letters from a Swift string?

From Dev

How can I pick from this string?

From Dev

How to pick up all numbers from string

From Dev

How to pick subset of strings that appear in longer string (in R)?

From Dev

Pick a value from an array randomly for a particular day

From Dev

Randomly pick elements from a vector of counts

From Dev

Pick a number randomly from two numbers

From Dev

Pick a value from an array randomly for a particular day

From Dev

Pick a number randomly from two numbers

From Dev

Android: Randomly pick images from 2 pools

From Dev

How to randomly pick one of multiple hostnames under one alias (~/.ssh/config)

From Dev

How to pick a random item from a arraylist in java?

From Dev

How to pick an item from list by strings trailing number

From Dev

how to fetch multiple sub strings from a long string

From Dev

How to get Median string value from array with multiple strings in Python?

From Dev

How to make python choose randomly between multiple strings?

Related Related

  1. 1

    How to randomly select a string between two strings from ENUM in java?

  2. 2

    How do I pick randomly from an array?

  3. 3

    How to pick one key from a dictionary randomly

  4. 4

    How to randomly get a string from a collection but prefer strings at the beginning of the collection

  5. 5

    How to display randomly selected strings from an array in java without repetitions

  6. 6

    How to randomly pick value from a list in Unity3D?

  7. 7

    How to randomly pick a number of combinations from all the combinations efficiently

  8. 8

    How to pick less than N objects randomly from K objects?

  9. 9

    randomly pick from 4 tables

  10. 10

    How to randomly assign strings from a macro to observations

  11. 11

    How to randomly assign strings from a macro to observations

  12. 12

    How to pick multiple images from device?

  13. 13

    How to pick multiple images from gallery?

  14. 14

    How to pick capital letters from a Swift string?

  15. 15

    How can I pick from this string?

  16. 16

    How to pick up all numbers from string

  17. 17

    How to pick subset of strings that appear in longer string (in R)?

  18. 18

    Pick a value from an array randomly for a particular day

  19. 19

    Randomly pick elements from a vector of counts

  20. 20

    Pick a number randomly from two numbers

  21. 21

    Pick a value from an array randomly for a particular day

  22. 22

    Pick a number randomly from two numbers

  23. 23

    Android: Randomly pick images from 2 pools

  24. 24

    How to randomly pick one of multiple hostnames under one alias (~/.ssh/config)

  25. 25

    How to pick a random item from a arraylist in java?

  26. 26

    How to pick an item from list by strings trailing number

  27. 27

    how to fetch multiple sub strings from a long string

  28. 28

    How to get Median string value from array with multiple strings in Python?

  29. 29

    How to make python choose randomly between multiple strings?

HotTag

Archive