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
Generate Heroku-like random names to use in your Java applications (github.com/atrox/haikunatorjava)
Any thoughts on why you went with a Map<String,Object> for configuration instead of a builder, a configuration object, or a set of constructors? I suspect you are using a Map in order to mimic Ruby's option hash, but this isn't idomatic Java and seems a bit error-prone. If you must use the Map for configuration, I'd at least provide some constants. Or an enumeration and change the configuration to Map<MyEnum,Object> to constrain the types. But I'd see about losing the Map in favor of something else.
Also, it would be nice to provide a way to actually change the words being used. Hard coding them into the app doesn't provide as much value. As a user, I should be able to configure the word lists.
Don't let this discourage you, keep working.
Edit: If you are only using one method (StringUtils.join) from Apache Commons, I'd consider dropping the dependency and writing my own join method. But that's fairly subjective and I don't like having many dependencies if I can avoid them.
More on reddit.comVideos
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!");