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:
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:
Random name: Ava Johnson
Hope this program is useful for your requirement. Visit back our blog for more Tutorials on Java Programming.