I am answering this very late, but this is what really useful for new reader. This is a very simple and efficient way to get random VALID names. To do so, add maven repository in POM.xml
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.12</version>
</dependency>
And then use the Faker class as below in your Java code
Faker faker = new Faker();
String name = faker.name().fullName();
String firstName = faker.name().firstName();
String lastName = faker.name().lastName();
String streetAddress = faker.address().streetAddress();
Try printing the result using standard System.out.println();
For more reference Faker Lib
Answer from Gaurav Lad on Stack OverflowI am answering this very late, but this is what really useful for new reader. This is a very simple and efficient way to get random VALID names. To do so, add maven repository in POM.xml
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.12</version>
</dependency>
And then use the Faker class as below in your Java code
Faker faker = new Faker();
String name = faker.name().fullName();
String firstName = faker.name().firstName();
String lastName = faker.name().lastName();
String streetAddress = faker.address().streetAddress();
Try printing the result using standard System.out.println();
For more reference Faker Lib
// class variable
final String lexicon = "ABCDEFGHIJKLMNOPQRSTUVWXYZ12345674890";
final java.util.Random rand = new java.util.Random();
// consider using a Map<String,Boolean> to say whether the identifier is being used or not
final Set<String> identifiers = new HashSet<String>();
public String randomIdentifier() {
StringBuilder builder = new StringBuilder();
while(builder.toString().length() == 0) {
int length = rand.nextInt(5)+5;
for(int i = 0; i < length; i++) {
builder.append(lexicon.charAt(rand.nextInt(lexicon.length())));
}
if(identifiers.contains(builder.toString())) {
builder = new StringBuilder();
}
}
return builder.toString();
}
natural language processing - Random name generator in Java - Code Review Stack Exchange
[Java] I wrote a random name generator
java - How To Randomly Generate From A Given List? - Stack Overflow
Super Simple Name Generator! - Shared Code - JVM Gaming
Videos
Hey there! I recently started learning java a couple weeks ago as my first language, mostly out of interest in developing some mods for minecraft. After getting comfortable with java, I intend to learn C# and pursue other interests involving game development.
At any rate, I've always loved coming up with unique names. So I thought why not challenge myself with writing a random name generator that doesn't just spit out nonsense. I feel comfortable calling the project complete for now although I could add more and more functionality, I do want to get on with continuing to learn.
I would appreciate feedback on my coding, even if it's a fairly simple project. Am I doing things moderately well? Does anything stand out as potentially problematic in the future if I carry on the way I have here? Am I writing too much useless or needless code? I am trying to ensure I don't solidify any bad habits or practices while I'm still learning fresh.
The project is at https://github.com/Vember/RandomNameGenerator
Greatly appreciate any feedback!
import java.util.*;
public class characters
{
public static void main(String[] args)
{
Random generate = new Random();
String[] name = {"John", "Marcus", "Susan", "Henry"};
System.out.println("Customer: " + name[generate.nextInt(4)]);
}
}
See how easy it is? I gave 4 simple names which can be replaced with words and such. The "4" in the code represents the number of names in the String. This is about as simple as it gets. And for those who want it even shorter(all I did was decrease the spacing):
import java.util.*;
public class characters{
public static void main(String[] args){
Random generate = new Random();
String[] name = {"John", "Marcus", "Susan", "Henry"};
System.out.println("Customer: " + name[generate.nextInt(4)]); }}
You can shuffle the ArrayList and take the first element,or iterate and take all of them in different order.
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class NameGenerator {
public static void main(String[] args) {
String[] peoples = {"Bob","Jill","Tom","Brandon"};
List<String> names = Arrays.asList(peoples);
Collections.shuffle(names);
for (String name : names) {
System.out.print(name + " ");
}
}
}
Otherwise you can create a random number each time and take a different name.
int index = new Random().nextInt(names.size());
String anynames = names.get(index);
System.out.println("Your random name is" + anynames + "now!");
Really new to Java, I appreciate all the constructive criticism you've got. My goal is to make a program that picks a name from a list, so I put them into an array, then added what I think should enable it to choose randomly, but I'm really stuck on getting the generator going. I'm using www.ideone.com to compile and execute it online but I keep getting errors. I found this post and I don't know if I just don't understand it or what, but it really didn't help me.
This is what I have so far:
String[] array = {"Sarah","Grace","Elizabeth","Jamie"};
Random ran = new Random();
System.out.println("You're up, " "!");I also found this question but I'm having a lot of trouble decoding the answer s/he got
Thanks so much!
Edit: formatting