Iram Shahed Iram Shahed - 1 month ago 6
Java Question

How to call random string from an array on Java?

String[] prompts = {"Describe to me in a sentence why this is a cool program.",
"Describe to me in a sentence how your day was.",
"Describe to me in a sentence what programming means to you.",
"Describe to me in a sentence why food is neccessary for humans."};
System.out.println(prompts);


I want to call a random string from the array how should I report it?

Answer

Just need to randomly generate an index in the range of your array length

int i = new Random().nextInt(prompts.length);
System.out.println(prompts[i]);
Comments