Home > Enterprise >  handling string length in javafaker
handling string length in javafaker

Time:09-29

i am using javafaker dependency to generate random Usernames for testing my application.But in my application the first name field doesn't accept names with less than 3 characters.Sometimes the names generated from java faker is less than 3 chars and validation message is thrown.So i want to generate names with string length greater than or equal to 3.

Maven Dependency

<dependency>
    <groupId>com.github.javafaker</groupId>
    <artifactId>javafaker</artifactId>
    <version>1.0.2</version>
</dependency>

Code

Faker faker = new Faker();
System.out.println(faker.name().firstName());

CodePudding user response:

Try this.

final Faker faker = new Faker();
String userName = faker.name().firstName();
final Set<String> usernames = new HashSet<>();
while (set.size() < len && userName.length() >= size) {
      usernames.add(userName);
      userName = faker.name().firstName();
}

CodePudding user response:

You can have a check for the length of the generated name and regenerate it if it is tripped. Check below code snippet.

Faker faker = new Faker();
String name = faker.name().firstName();
while (name.length() <= 3) {
    name = faker.name().firstName();
}
  •  Tags:  
  • java
  • Related