Print random message or string from an array in java

Print random message or string from an array in java
---------------------------------------------------------------------------------


import java.util.Random;

public class RandomFortuneService  {
public static void main(String[] args) {

// create an array of strings
String[] data = 
"Beware of the wolf in sheep's clothing",
"Diligence is the mother of good luck",
"The journey is the reward"
};

// create a random number generator
 Random myRandom = new Random();

System.out.println(myRandom);
int index = myRandom.nextInt(data.length);
System.out.println(index);
String theFortune = data[index];

System.out.println(theFortune);


}
}


#generate random number
#java random number generator
#print random values from an array

Comments

Popular posts from this blog

Git Commands With Output Log

Java Interview Preparation (conceptual)

Java 8 Function Interface