In this post, I will show you how to write a simple Java Program to generate Random First & Last Names and print them on the screen. I am going to use the Java Random library to generate the names. So, let’s see how to do it.
Here is the complete Java code to print Random Names:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Random; /** * * @author agurc */ public class RandomNames { // define few firstNames and lastNames private static String[] firstNames = {"John", "Emma", "Olivia", "Ava", "Isabella", "Sophia", "Robin"}; private static String[] lastNames = {"Doe", "Smith", "Johnson", "Williams", "Jones", "Brown", "Hood"}; // create an obj private static Random random = new Random(); public static void main(String[] args) { // generate a Random First & Last Name and print it String randomName = firstNames[random.nextInt(firstNames.length)] + " " + lastNames[random.nextInt(lastNames.length)]; System.out.println("Random name: " + randomName); } } |
Output:
1 |
Random name: Ava Johnson |
Hope this program is useful for your requirement. Visit back our blog for more Tutorials on Java Programming.